[28357] in Perl-Users-Digest
Perl-Users Digest, Issue: 9721 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Sep 13 14:05:49 2006
Date: Wed, 13 Sep 2006 11:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 13 Sep 2006 Volume: 10 Number: 9721
Today's topics:
Re: Auto Web Surfing / Form Entry <tadmc@augustmail.com>
Boolean Regexp with perlre <caoimhinocrosbai_at@yahoo.com>
comparing modification times of files <nobody@dizum.com>
Re: FAQ 9.7 How do I make a pop-up menu in HTML? <brian.d.foy@gmail.com>
Re: getgrgid with multiple occurances of gid in groupfi <aukjan@vanbelkum.no.spam.nl>
Re: How do you compress a url for ascii output? <tzz@lifelogs.com>
Re: How to make only one thread sleep? <ralmoritz@gmail.com>
Re: How to make only one thread sleep? <1usa@llenroc.ude.invalid>
How to put a single thread to sleep? <ralmoritz@gmail.com>
Re: How to put a single thread to sleep? <1usa@llenroc.ude.invalid>
Re: How to put a single thread to sleep? <jurgenex@hotmail.com>
Re: How to put a single thread to sleep? xhoster@gmail.com
Re: How to put a single thread to sleep? <bik.mido@tiscalinet.it>
Re: i need to ssh to remote server using Perl <christoph.lamprecht.no.spam@web.de>
Re: I want to output all of my STDOUT to a single line. <tzz@lifelogs.com>
Inserting into a database <mattjones@hotmail.co.uk>
Re: Integrate Compress::Zlib seamlessly w/no compressio peter.j.torelli@gmail.com
not /pattern/ <mattjones@hotmail.co.uk>
not /pattern/ <mattjones@hotmail.co.uk>
Re: not /pattern/ <1usa@llenroc.ude.invalid>
Re: not /pattern/ <tadmc@augustmail.com>
not pattern <mattjones@hotmail.co.uk>
Re: not pattern <David.Squire@no.spam.from.here.au>
Re: Search and replace problem <nobull67@gmail.com>
Re: Search and replace problem <nobull67@gmail.com>
String buffer instead of file handle? <danparker276@yahoo.com>
Re: String buffer instead of file handle? <mritty@gmail.com>
Re: String buffer instead of file handle? <nobull67@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 13 Sep 2006 08:22:32 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Auto Web Surfing / Form Entry
Message-Id: <slrnegg1go.8v4.tadmc@magna.augustmail.com>
frznchckn@gmail.com <frznchckn@gmail.com> wrote:
> I'm a novice Perl programmer but an intermediate coder in general and
Then you should be aware of the concept of checking a FAQ before posting...
> am hoping someone can point me in the right direction on this...
Subject: Auto Web Surfing / Form Entry
^^^^ ^^^^
^^^^ ^^^^
perldoc -q form
perldoc -q auto
How do I automate an HTML form submission?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 13 Sep 2006 19:29:47 +0200
From: Kevin Crosbie <caoimhinocrosbai_at@yahoo.com>
Subject: Boolean Regexp with perlre
Message-Id: <45083f01$0$19201$88260bb3@news.teranews.com>
Hi,
I'm not sure if this is appropriate for this group, it's related to
perlre rather than perl, if so, apologies and I'd appreciate a pointer
to the right group.
I'm trying to find out if this is possible before I embark upon
implementing a solution to my problem.
I have a string with comma separated tags:
"a, b, c, d, e, f"
It's rather easy to write something to express a boolean OR:
a OR b OR c = (^|,(\s)+)(a|b|c)[,$]
What I would like to know is if there is a way to express AND or NOT:
1. (a OR b) AND c
2. (a OR b) AND NOT c
3. (a OR b) AND NOT (c OR d)
I imagine there is no nice way to do this without doing something like
writing out your AND clause before and after whatever OR clause you are
using, which would become really messy for more complicated expressions,
but perhaps someone knows of some way to do this.
Regards,
Kevin
------------------------------
Date: Wed, 13 Sep 2006 17:10:06 +0200 (CEST)
From: Nomen Nescio <nobody@dizum.com>
Subject: comparing modification times of files
Message-Id: <a8e2005dfa6570267220e991d7a751e8@dizum.com>
Is this the right way to compare file timestamps?
if (-M $file >= -M $otherFile) {
# $file is not newer than $otherFile
}
And I sometimes get *modified* and *changed* mixed up...contents get
modified, permissions get changed, right?
thanks
------------------------------
Date: Wed, 13 Sep 2006 11:17:43 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 9.7 How do I make a pop-up menu in HTML?
Message-Id: <130920061117430165%brian.d.foy@gmail.com>
In article <9s2eg2t703b7262vd1dsoiptbntja2e8b2@4ax.com>, Michele Dondi
<bik.mido@tiscalinet.it> wrote:
> In view of all this, changing the Subject to the effect of including a
> reference to CGI.pm would not be much of a solution, since typically
> one who already knows what it is or even has a vague idea has smaller
> chances of actually asking the question.
Indeed. People don't ask "How do I make a pop-up menu with CGI.pm?".
I could rephrase it to be "How do I make an HTML pop-up menu in Perl?"
> Rather, a remark that the question is not strictly on topic...
As someone already mentioned, the FAQ is much larger than this group,
so I won't include anything about topicality in the answer. :)
--
Posted via a free Usenet account from http://www.teranews.com
------------------------------
Date: Wed, 13 Sep 2006 15:22:10 +0200
From: Aukjan van Belkum <aukjan@vanbelkum.no.spam.nl>
Subject: Re: getgrgid with multiple occurances of gid in groupfile
Message-Id: <96f44$45080602$c2abfc64$9123@news2.tudelft.nl>
Dave Weaver wrote:
> Glad to hear you solved your problem. Perhaps some tips would be
> welcome too?
sure .. always welcome! Thanks!
>> while ( my ( $name,$pw,$gid,$members ) = getgrent ){
>
> If you're not using all the values returned, why created variables to
> store them?
> while ( my ($name, $gid, $members) = (getgrent)[0, 2, 3] ) {
> or
> while ( my ( $name, undef, $gid, $members ) = getgrent() ){
>
I do use these variables in the rest of the code, but yes .. this is how
I normally do it....
>> $gr_by_gid{$gid} = {} unless exists $gr_by_gid{$gid};
>
> There is no need to create this anonymous hashref. Perl's
> autovivification feature will create this for you. (i.e. you can
> simply remove this line from your code). See `perldoc perlref` for
> information on autovivification.
I have had some problems with autovivication in the past (in combination
with Config ::General if I recall correctly), and this was needed to
solve the problem... So this got stuck in my mind/code. But you're right
, I don't need it!
>
>> $gr_by_gid{$gid}{$_} = $name foreach ( split /\s+/,$members);
>
> Again, a personal style choice, but I'd leave off the unnessesary
> parentheses, and use the shorter 'for':
>
> $gr_by_gid{$gid}{$_} = $name for split /\s+/,$members;
>
For me it is a matter of making the grouping clear for the other users
of my code (who are not that adapt with Perl). So in some cases I leave
them in.
------------------------------
Date: Wed, 13 Sep 2006 11:51:59 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: How do you compress a url for ascii output?
Message-Id: <g69odtj228w.fsf@CN1374059D0130.kendall.corp.akamai.com>
On 11 Sep 2006, blaine@worldweb.com wrote:
> Sorry, maybe I was not clear.
>
> My url is actually not 256 char's. It's actually very short, however
> there is encrytped variable data that I would like to shorten.
>
> ie.
> www.testurl.com/script.html?encryptedString=CIAQEDEUDVvVEwEEChDtDaAVCHAhBFANADBeAlEqCvDrCgDHDeDyClBREgBSCFEdBIBbDeCYACBLEWAwEaEECMBjBvCEBiEbCCCgABEAElBNCsEYDFCODEEoECDaDgADCtEhBpAOCACGDECsCfBsCPAaBhAaDeDTECERBdEIBuEiEjDqCgEQBPDjBQAEEjDtDFCMDaDfAuCwEfChCmEmDtDRDlBdBYACBkDYEpEnDoCMAJETEcDxEC
>
>
> So I would like a much shorter encryptedString part
>
> ie
> www.testurl.com/script.html?encryptedString=<40 - 60% shorter then the
> original>
If you want to make the URL self-sufficient, use a database table.
http://www.testurl.com/script.html?encryptedString=[UNIQUE ID HERE]
and then on the server save this string with a unique ID in a 2-column
database table. You can use sqlite or something similar if you don't
already have a database server running, and want the operation to be
fast. Just increment the ID when a new string is added.
If you expect lots of strings, prune the database table occasionally
or on a schedule (though this, of course, depends on your setup and
needs).
Ted
------------------------------
Date: 13 Sep 2006 06:05:22 -0700
From: "Ralph Moritz" <ralmoritz@gmail.com>
Subject: Re: How to make only one thread sleep?
Message-Id: <1158152721.972895.225950@e3g2000cwe.googlegroups.com>
A. Sinan Unur wrote:
> "Ralph Moritz" <ralmoritz@gmail.com> wrote in
> news:1158137271.507323.263550@h48g2000cwc.googlegroups.com:
>
> >> My program is included below in it's entirety.
>
> Your program has too many external dependencies for anyone else to try
> to debug it. The need to post the shortest possible program that still
> exhibits the problem.
There are no external dependencies. I posted the entire program.
> There are oddities such as defining a subroutine warn with the name of a
> builtin. Again, I don't know if this has anything to do with the problem
> or even if it is a problem but why create complications?
I don't know what you mean. There is no definition for a subroutine
called warn() in the code I posted. ??
> As far as I can see, your factory_loop does not limit the number of
> threads it is creating.
Not explicitly, but if you look closely you'll see that it's calling
th_sem->down which decrements the semaphore's count by one.
If the count were to drop below zero, the semaphore blocks until
the count is raised again. This effectively makes factory_loop block
if the maximum number of worker threads are already running.
--
Ralph Moritz
------------------------------
Date: Wed, 13 Sep 2006 14:08:28 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to make only one thread sleep?
Message-Id: <Xns983D6728756BEasu1cornelledu@132.236.56.8>
"Ralph Moritz" <ralmoritz@gmail.com> wrote in
news:1158152721.972895.225950@e3g2000cwe.googlegroups.com:
> A. Sinan Unur wrote:
>> "Ralph Moritz" <ralmoritz@gmail.com> wrote in
>> news:1158137271.507323.263550@h48g2000cwc.googlegroups.com:
>>
>> >> My program is included below in it's entirety.
>>
>> Your program has too many external dependencies for anyone else to try
>> to debug it. The need to post the shortest possible program that still
>> exhibits the problem.
>
> There are no external dependencies.
Yes it does.
> I posted the entire program.
But one would need to create a config file, directories and files to be
able to run it. Reduce the program to something which still exhibits your
problem, and which others can run just by copying and pasting.
>> There are oddities such as defining a subroutine warn with the name of
>> a builtin. Again, I don't know if this has anything to do with the
>> problem or even if it is a problem but why create complications?
>
> I don't know what you mean. There is no definition for a subroutine
> called warn() in the code I posted. ?
I confused myself in an attempt to reduce your program to something that
can be run.
>> As far as I can see, your factory_loop does not limit the number of
>> threads it is creating.
>
> Not explicitly, but if you look closely you'll see that it's calling
> th_sem->down which decrements the semaphore's count by one.
> If the count were to drop below zero, the semaphore blocks until
> the count is raised again. This effectively makes factory_loop block
> if the maximum number of worker threads are already running.
It looks like my efforts have been wasted. Did you actually run the
program I posted? Does it work on your system as it does on mine? If so,
we can rule out any problems with sleep.
Now, it is your turn to do some work: Extend that program to what you
need in a step-by-step approach: Add one thing, test, add another, test.
You will find the solution to your problem. If not, you will at least
have come up with a program which others can use to help you.
Sinan
> --
> Ralph Moritz
PS: Use the correct format for the signature separator. It is "dash-dash-
space-newline"
------------------------------
Date: 13 Sep 2006 06:44:10 -0700
From: "Ralph Moritz" <ralmoritz@gmail.com>
Subject: How to put a single thread to sleep?
Message-Id: <1158155050.341951.191020@p79g2000cwp.googlegroups.com>
Hi,
On my system (Windows XP Prof SP2), calling Perl's sleep()
from a thread appears to put the entire process to sleep, rather
than just the current thread.
Q: How can I put just the current thread to sleep?
TIA,
Ralph
--
Ralph Moritz
Ph: +27 84 626 9070
GPG Public Key: http://ralphm.info/me@ralphm.info.gpg
------------------------------
Date: Wed, 13 Sep 2006 14:29:45 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to put a single thread to sleep?
Message-Id: <Xns983D6AC5DB68Aasu1cornelledu@132.236.56.8>
"Ralph Moritz" <ralmoritz@gmail.com> wrote in
news:1158155050.341951.191020@p79g2000cwp.googlegroups.com:
> On my system (Windows XP Prof SP2), calling Perl's sleep()
> from a thread appears to put the entire process to sleep, rather
> than just the current thread.
And where is the code that shows that?
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
use threads;
sub sleeper {
my $t0 = time;
while ( 1 ) {
print "\n ... Wovzah! ", time - $t0, " ... Yeee-haw!\n";
sleep 10;
}
}
sub worker {
while ( 1 ) {
sleep 1;
print '*';
}
}
my $s = threads->create('sleeper');
my $w = threads->create('worker');
$_->detach for $s, $w;
sleep 1 while 1;
> Q: How can I put just the current thread to sleep?
You know, posting the same nitwit question in multiple threads is not a
good way of eliciting help.
> TIA,
Don't thank me if you do not appreciate my help.
> Ralph
>
> --
> Ralph Moritz
I see, you still haven't learned how to use a sig separator.
> Ph: +27 84 626 9070
I am tempted.
Sinan
------------------------------
Date: Wed, 13 Sep 2006 14:47:27 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How to put a single thread to sleep?
Message-Id: <3SUNg.51765$Qb2.11720@trnddc08>
Ralph Moritz wrote:
> Q: How can I put just the current thread to sleep?
Is this question different to the one that is currently discussed in the
thread "How to make only one thread sleep?"
jue
------------------------------
Date: 13 Sep 2006 15:34:27 GMT
From: xhoster@gmail.com
Subject: Re: How to put a single thread to sleep?
Message-Id: <20060913113521.041$gS@newsreader.com>
"Ralph Moritz" <ralmoritz@gmail.com> wrote:
> Hi,
>
> On my system (Windows XP Prof SP2), calling Perl's sleep()
> from a thread appears to put the entire process to sleep, rather
> than just the current thread.
>
> Q: How can I put just the current thread to sleep?
I don't see that on my machine.
H:\>perl -le "use threads; $t = async { 1 foreach 1..1e6; warn $_ foreach
1..10; }; warn q(sleep) ; sleep 3; warn q(awake); $t->join"
sleep at -e line 1.
1 at -e line 1.
2 at -e line 1.
3 at -e line 1.
4 at -e line 1.
5 at -e line 1.
6 at -e line 1.
7 at -e line 1.
8 at -e line 1.
9 at -e line 1.
10 at -e line 1.
awake at -e line 1.
H:\>perl -v
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 13 Sep 2006 18:37:55 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to put a single thread to sleep?
Message-Id: <5pcgg2tq4bdnvvgtrh2k46b1bb9j5sposg@4ax.com>
On 13 Sep 2006 06:44:10 -0700, "Ralph Moritz" <ralmoritz@gmail.com>
wrote:
>Q: How can I put just the current thread to sleep?
A: as already shown to you by another poster.
BTW: reposting the same exact question in another thread is not going
to help you!
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Wed, 13 Sep 2006 15:12:41 +0200
From: Ch Lamprecht <christoph.lamprecht.no.spam@web.de>
Subject: Re: i need to ssh to remote server using Perl
Message-Id: <ee9049$du8$1@online.de>
vish.chitnis@gmail.com wrote:
> Ch Lamprecht wrote:
>
>>vish.chitnis@gmail.com wrote:
>>
>>>hi paul,
>>>
>>>i get this error when i try to install the Net::SSH::Perl package.. wht
>>>else i need to do in order to install this package successfully
>>>
>>>vish
>>>
>>> nmake -- NOT OK
>>>unning make test
>>> Can't test without successful make
>>>unning make install
>>> make had returned bad status, install seems impossible
>>>
>>
>>Why don't you use ppm to install the package:
>
> ok.i found that PPM is there in /perl/bin its ms-dos batch file which
> after run gives me the ppm command prompt..now when i issue the above
> command
> i net-ssh-perl to install this package, i get package not found
> error..
type
help rep
from the ppm-prompt to find out, how to add repositories.
Then add the following address to the list:
http://theoryx5.uwinnipeg.ca/cgi-bin/ppmserver?urn:/PPMServer58
HTH, Christoph
--
perl -e "print scalar reverse q/ed.enilno@ergn.l.hc/"
------------------------------
Date: Wed, 13 Sep 2006 12:32:19 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: I want to output all of my STDOUT to a single line....dont want to scroll
Message-Id: <g69wt87zq0c.fsf@CN1374059D0130.kendall.corp.akamai.com>
On 11 Sep 2006, seancsnyder@gmail.com wrote:
> I want my program to simply output to a single line in STDOUT. In
> other words i want to print a line, then 'backspace' that line and
> print somethign new on the same line. I do not want the terminal
> window to scroll.
Besides the \r technique shown in this thread, you could also clear
the screen.
On Unix machines, you can often do it with the `/usr/bin/clear'
program. IIRC on Windows it's `cls' or something similar.
This is definitely slower than \r, so if your data updates quickly it
won't look nice. It does look better in some circumstances, though
(e.g. your data is longer than the screen width).
Ted
------------------------------
Date: 13 Sep 2006 08:05:28 -0700
From: "MattJ83" <mattjones@hotmail.co.uk>
Subject: Inserting into a database
Message-Id: <1158159928.876303.202540@m73g2000cwd.googlegroups.com>
Hi,
Im currently inserting data into a database from a log file. This is
done by matching words from a log file and placing the line of the log
file into the database field. This is repeated for each log file in the
directory.
Currently I can get the information into the correct fields of the
database.
My problem is when a word is not matched in the log file that the
script is looking through and thus, not placed into the database field.
As a result, the next line of information is not then imported into the
log file when it should be! The script should not match the previous
word - leave the field in the database blank and then carry on with the
pattern matching - filling in the remaining fields.
Current code (not working code because i think you need to have a
database to understand the problem - and im not sure how placing
working code (without database) would help!:
#!/usr/central/bin/perl
use strict;
use warnings;
use DBI;
my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
{
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
my ($info, $elapsed1, $fast, $elapsed2, $inversions, $elapsed3 );
while(<$LOG>)
{
chomp;
{
if (/updates table/)
{
print "$_\n";
$info = $_;
}
if (/elapsed/)
{
print "[$_]\n";
if ($inversions)
{
$elapsed3 = $_;
}
elsif ($fast)
{
$elapsed2 = $_;
}
elsif ($info)
{
$elapsed1 = $_;
}
last;
}
}
if (/FASTSEARCH|conflicting/)
{
print "$_\n";
$fast = $_;
}
if (/inversions/)
{
print "$_\n";
{
$inversions = $_;
}
}
}
my $dbh = DBI ->connect("dbi:Oracle:server", "database", "password")
or die "couldn't connect to database: $DBI::errstr\n";
$dbh->do("INSERT INTO TEST2 (sfd, w, sdf, hfg, rt, bv, te)
VALUES (?, ?, ?, ?, ?, ?, ?)", undef,
$filename, $info, $elapsed1, $fast, $elapsed2, $inversions,
$elapsed3);
}
When i change 'elsif ($fast)' to ($fast or " ") it solves the problem
(includes the field after the blank field) but it leaves the whole
column of elapsed1 blank - is this because it is in a loop and can only
do 1 thing or another but not both? Is there a way to solve this?
I have also tried changing the last elsif statement to 'else' but to no
avail.
Thanks
------------------------------
Date: 13 Sep 2006 10:20:42 -0700
From: peter.j.torelli@gmail.com
Subject: Re: Integrate Compress::Zlib seamlessly w/no compression?
Message-Id: <1158168042.654081.99040@e63g2000cwd.googlegroups.com>
Paul,
I really like your solution, it is very clean and fits with my style.
Thank you.
The idea of writing another abstraction layer to handle this simple
case (IMHO) is preposterous as I would have to tote this baggage around
indefinitely. This sol'n adds a few extra lines of code, but that's
way cleaner than supporting another object.
--Peter
Paul Marquess wrote:
> IO::Zlib or PerlIO::gzip are the best approaches to use at the moment.
>
> if (want compressed file) {
> $fh = IO::Zlib->new($filename, "wb9");
> }
> else {
> $fh = open ">$filename";
> }
>
> die "error ... error"
> unless $fh;
>
> If you want to get into manipulating the gzip header, you could use
> IO::Compress::Gzip, but that's still beta code.
>
> Paul
------------------------------
Date: 13 Sep 2006 09:15:39 -0700
From: "MattJ83" <mattjones@hotmail.co.uk>
Subject: not /pattern/
Message-Id: <1158164139.103844.196250@d34g2000cwd.googlegroups.com>
Quick question
if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?
Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????
Thanks
------------------------------
Date: 13 Sep 2006 09:15:45 -0700
From: "MattJ83" <mattjones@hotmail.co.uk>
Subject: not /pattern/
Message-Id: <1158164144.791478.157590@b28g2000cwb.googlegroups.com>
Quick question
if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?
Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????
Thanks
------------------------------
Date: Wed, 13 Sep 2006 16:37:01 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: not /pattern/
Message-Id: <Xns983D8058FE0F1asu1cornelledu@132.236.56.8>
"MattJ83" <mattjones@hotmail.co.uk> wrote in news:1158164144.791478.157590
@b28g2000cwb.googlegroups.com:
> } elsif /!word/ { exit; } # ????
This group is for discussing Perl only. That is not Perl code. Please post
Perl code.
Sinan
------------------------------
Date: Wed, 13 Sep 2006 11:35:49 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: not /pattern/
Message-Id: <slrneggcr5.b12.tadmc@magna.augustmail.com>
MattJ83 <mattjones@hotmail.co.uk> wrote:
> Quick question
It is a quick question that you could answer yourself by
writing a quick program to test your theory.
> if i was to write code to match a /pattern/ but then wanted the script
> to exit if it didn't find the pattern is it as simple as (!/pattern/) ?
What happened when you tried it?
> Code:
> {
> if (/word/)
> {
> print "$_\n";
> $info = $_;
> } elsif /!word/ { exit; } # ????
^^^^^^^
That is not Perl code. (and you put the ! in the wrong place)
This is Perl code:
----------------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = 'this has a Word in it';
if (/word/) {
print "$_\n";
}
elsif ( ! /word/ ) {
print "here I would be calling exit()\n";
}
----------------------
See how easy that was?
No need to ask hundreds of people to help with something that simple...
But I would write it this way instead:
----------------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = 'this has a Word in it';
exit unless /word/;
print "$_\n"; # must have matched for control to reach this statement
----------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 13 Sep 2006 09:15:34 -0700
From: "MattJ83" <mattjones@hotmail.co.uk>
Subject: not pattern
Message-Id: <1158164134.454836.17500@e63g2000cwd.googlegroups.com>
Quick question
if i was to write code to match a /pattern/ but then wanted the script
to exit if it didn't find the pattern is it as simple as (!/pattern/) ?
Code:
{
if (/word/)
{
print "$_\n";
$info = $_;
} elsif /!word/ { exit; } # ????
Thanks
------------------------------
Date: Wed, 13 Sep 2006 17:34:52 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: not pattern
Message-Id: <ee9bvd$49g$1@gemini.csx.cam.ac.uk>
MattJ83 wrote:
> if i was to write code to match a /pattern/ but then wanted the script
> to exit if it didn't find the pattern is it as simple as (!/pattern/) ?
Yes, but that is not what you have written below.
> Code:
> {
> if (/word/)
> {
> print "$_\n";
> $info = $_;
> } elsif /!word/ { exit; } # ????
First, you don't need an 'elsif', you just want an 'else' since your
second test is (meant to be) just the negation of the first. Secondly,
you need the ! (not) to be outside the pattern. Please try out your code
before posting it.
The example below illustrates these points.
----#!/usr/bin/perl
use strict;
use warnings;
while (<DATA>) {
print;
if (!/g/) {
print "\tNo 'g'\n";
}
else {
print "\tContains 'g'\n";
}
}
__DATA__
cat
dog
frog
bird
----
Output:
cat
No 'g'
dog
Contains 'g'
frog
Contains 'g'
bird
No 'g'
------------------------------
Date: 13 Sep 2006 10:44:55 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Search and replace problem
Message-Id: <1158169495.298762.89740@d34g2000cwd.googlegroups.com>
ironmanda@yahoo.com wrote:
> I'm trying to build a general way of handling search and replace
> of strings. The regexp in the commented out part works perfectly.
> But, the foreach part doesn't work. The thing is, \1 is being
> interpreted literally. Any ideas?
Others have pointed out that you meant $1 not \1 and also that Perl
would have told you this if you'd let it so I'm going to take it as
read that \1 as been replaced by $1.
You are not the first person to post here with substancially the same
desire.
Indeed at YAPC::Europe::2004 I gave a lightning talk about all the
different varients of this thread that I'd come accross over the
preceding couple of years.
http://birmingham.pm.org/talks/brian/faq-short/Intro.html
> #!/usr/local/bin/perl
>
> my $str="http://www.xxx.com/yyy/zzz/25331/2";
>
> $repurl{"([0-9]*)\\/[0-9]+\$"}="\\1";
You are not using the natural representation of things.
You never look things up in %repurl by key so a hash is not the natural
data structure. It is just a list of pairs. So the natural
representation is more like:
push @repurl, [ "([0-9]*)\\/[0-9]+\$" => "\$1" ];
But the natural representation of a regex for later evaluation is not a
string, it's a precompiled regex. And the natural representation of an
expression for later evaluation is not a string either, it's precomiled
code.
push @repurl, [ qr/([0-9]*)\/[0-9]+\$/ => sub { $1} ];
foreach my $subst (@repurl) {
$str =~ s/$subst->[0]/$subst->[1]->()/eg;
}
But of course fundamentally you have to ask yourself why you are trying
to write something "general" when you started off with something
general?
Why not just write Perl? Be aware that any solution that ends up using
eval() untilately allows any Perl anyhow so you are not restricting
what can be done.
Why not simply express what you want in Perl in the first place rather
than inventing your own "small language" that isn't any easier to write
than Perl nor is it more restrictive than Perl.
push @repurl, sub { s/([0-9]*)\/[0-9]+\$/$1/g };
foreach my $subst (@repurl) {
$subst->() for $str;
}
------------------------------
Date: 13 Sep 2006 10:51:09 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: Search and replace problem
Message-Id: <1158169869.551461.23360@i3g2000cwc.googlegroups.com>
John W. Krahn wrote:
> my %repurl = ( q[(\d*)/\d+$] => q[$1] );
> $str =~ s/$key/$repurl{$key}/eeg;
Be aware that this is interpreting the q[$1] as a simple Perl
expression to be evaluated rather than as a string to undergo
interpolation.
If the replacement had been q[foo$1] then this would have caused a
runtime error but because it would be inside an eval() and you are not
checking $@ the error would simply be ignored and the replacement would
be null.
I prefer to use
# http://birmingham.pm.org/talks/brian/faq-short/Fixed.html
s{$key}{
chop( my $s = eval "<<_END_\n$repurl{$key}\n_END_" );
die $@ if $@;
$s;
}eg;
------------------------------
Date: 13 Sep 2006 10:36:45 -0700
From: "danparker276@yahoo.com" <danparker276@yahoo.com>
Subject: String buffer instead of file handle?
Message-Id: <1158169005.811396.291690@m73g2000cwd.googlegroups.com>
I have to connect to this module, and I usually send it a file handle,
or stdout. But I want to write it to a string. Is there a string
buffer or something I can use?
open (FILEO, ">test.txt");
$xw = MP3Com::XMLWriter->new (\*FILEO);
or
$xw = MP3Com::XMLWriter->new (\*STDOUT);
I want to do:
my $xmlstring
$xw = MP3Com::XMLWriter->new ($xmlstring);
------------------------------
Date: 13 Sep 2006 10:54:19 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: String buffer instead of file handle?
Message-Id: <1158170059.534374.210770@p79g2000cwp.googlegroups.com>
danparker276@yahoo.com wrote:
> I have to connect to this module, and I usually send it a file handle,
> or stdout. But I want to write it to a string. Is there a string
> buffer or something I can use?
Yes.
> open (FILEO, ">test.txt");
> $xw = MP3Com::XMLWriter->new (\*FILEO);
>
> or
> $xw = MP3Com::XMLWriter->new (\*STDOUT);
>
> I want to do:
> my $xmlstring
> $xw = MP3Com::XMLWriter->new ($xmlstring);
Check the documentation for the function you're using. In this case,
that's open:
perldoc -f open
[snip]
File handles can be opened to "in memory" files held
in Perl scalars via:
open($fh, '>', \$variable) || ..
[snip]
Paul Lalli
------------------------------
Date: 13 Sep 2006 10:55:58 -0700
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: String buffer instead of file handle?
Message-Id: <1158170158.342060.161040@d34g2000cwd.googlegroups.com>
danparker276@yahoo.com wrote:
> I have to connect to this module, and I usually send it a file handle,
> or stdout. But I want to write it to a string. Is there a string
> buffer or something I can use?
Yes.
> open (FILEO, ">test.txt");
perldoc -f open
(At least on recent Perl. On older Perl you can't just use open() to
make a scalar behave like a file and you need the IO::Scalar module).
BTW: If you _do_ have a recent Perl then you should accept the standard
admonishment to read the documentation for the functions you are using
before you start asking others for help.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9721
***************************************