[19093] in Perl-Users-Digest
Perl-Users Digest, Issue: 1288 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 11 18:10:40 2001
Date: Wed, 11 Jul 2001 15:10:23 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <994889422-v10-i1288@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 11 Jul 2001 Volume: 10 Number: 1288
Today's topics:
Re: inline average <goldbb2@earthlink.net>
Re: my $ref; vs. my $ref = {}; (Anno Siegel)
openssl (Mike)
Re: passing quotes to the command line <gnarinn@hotmail.com>
Re: passing quotes to the command line <goldbb2@earthlink.net>
Re: problem with data.. <ddunham@redwood.taos.com>
Re: problem with data.. <iltzu@sci.invalid>
Re: puzzled over fork() doing something different to bo <goldbb2@earthlink.net>
Side effect of > operator? <I.Stevens@btinternet.com>
Re: Side effect of > operator? <quanyi.sun@nokia.com>
Re: Simple pattern-matching problem (E.Chang)
Re: Simple pattern-matching problem (E.Chang)
Re: Simple pattern-matching problem (Tad McClellan)
Re: Simple pattern-matching problem <krahnj@acm.org>
Socket programming problem <leed@ucdavis.edu>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 11 Jul 2001 16:44:38 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: inline average
Message-Id: <3B4CBAB6.D05F2E6D@earthlink.net>
Rich wrote:
>
> "David Frauzel" <nogard@gnosrehtaew.ten> wrote in message
> news:9ho2ub$rvu$1@news.aros.net...
> > Thanks everyone.
> >
> > Looks as though the concensus is there are two ways, more or less,
> > to get it done. One is to tweak at the mechanics behind hashes
> > themselves (with tie tricks); but for some reason that makes me
> > queasy. It feels like changing the Laws of Physics just to get 1 and
> > 1 to equal 3. ;} I know, I know, picky picky. Beggars choosers. All
> > that. I am grateful for the examples, it's something I know I'll
> > want to use eventually.
>
> It's not really tweaking the mechanics of the hash. The example I gave
> tied the hash value because you specified the use of a hash value.
> There could just as easily been a $values_average scalar which could
> have produced the same output.
>
> use Average; #"Average" code from previous post
>
> my %values = (
> red => 5.0,
> orange => 4.0,
> yellow => 5.5,
> );
>
> my $values_average;
>
> tie ($values_average, "Average", \%values);
>
> print "The average is $values_average"; #prints 4.83
>
> Or, you could also copy the object to access methods from the
> "Average" class.
>
> $object = tie ($values_average, "Average", \%values);
>
> print "The average is $values_average"; # same as before
>
> Or, you can access the methods directly.
>
> my $avg = $object->FETCH();
>
> print "The average is $avg"; #$avg is 4.83
Or possibly:
tie $values{average}, "Average", \%values;
This gives all the benefits of a tied scalar, without having to create
another variable for it... and it's only one value that's tied, not the
whole hash.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: 11 Jul 2001 18:59:50 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: my $ref; vs. my $ref = {};
Message-Id: <9ii7n6$maa$1@mamenchi.zrz.TU-Berlin.DE>
According to Benjamin Goldberg <goldbb2@earthlink.net>:
> [Please don't top-post and don't quote signatures unless you're
> commenting on them.]
>
> Arvin Portlock wrote:
> > Philip Newton <pne-news-20010620@newton.digitalspace.net> wrote in
> > message news:pvj1jtc86emusud4er8ov4c7j2proe21gc@4ax.com...
[snipparone]
> > My problems in the past with declaring plain 'my $hashref;' were along
> > the lines that sometimes such a test would still return true even if
> > no keys seemed to have been created. I wondered whether perl would
> > sometimes turn $hashref into an actual reference to a hash based on
> > code in other areas of the subroutine, i.e., just the fact that the
> > subroutine contained $hashref-> *anywhere* would be enough for it to
> > do this.
Yes, that's auto-vivification.
> When a scalar will autovivify is a confusing issue. Perhaps someone
> could explain it in some detail?
Auto-vivification isn't that difficult, it's really very DWIMish.
Because it normally does its thing silently in the background, it
can be hard to think of it when it happens in unexpected ways.
Whenever an lvalue (a place that can hold a scalar) is used as if
it held a reference to something, a reference to that kind of something
is created and placed there. (If a non-lvalue is treated this way,
it *must* hold the right kind of reference.) This applies recursively
and is most useful with nested data structures, so that a programmer
doesn't have to worry about initialization of intermediate stages.
It may come as a surprise, however, when a top-level variable, supposed
to hold a reference of some kind, springs to life this way:
my $hashref;
...
if ( $hashref->{ somekey} ) {
...
}
...
if ( defined $hashref ) {
# oops, we never assigned to $hashref
}
So yes, even mentioning a variable in a certain context, without
actually intruding and assigning to it, can make the variable react
strange. They're funny that way.
Anno
------------------------------
Date: 11 Jul 2001 13:39:32 -0700
From: michael_of_neb@yahoo.com (Mike)
Subject: openssl
Message-Id: <5a10590e.0107111239.16c4737f@posting.google.com>
Hi.
I have been trying to get openSSL to work on my system(s).
The Active state version of Perl on my Windows 98 box, version
ActivePerl-5.6.0.623-MSWin32-x86-multi-thread.msi
has not presented any other significant problems so far..
BUT
as a part of the installation on the win32 platform
of the OpenSSL in Windows environment
AFTER
I have done
>perl configure vc-win32
and
>ms\do_masm
when I try to do:
c:\progra~1\micros~4\vc98\bin\nmake.exe -f ms\ntdll.mak
I get an error at the point where it is trying to build
.\crypto\cryptlib.c(59): fatal error c1083: Cannot open include file
'stdio.h'
:No such file or directory
Nmake: fatal error u1077:'c:\progra~1\micros~4\vc98\bin\cl.exe':
return mode
'0x2'
Stop
By the way-
The path contains the following:
path=C:\WINDOWS;C:\WINDOWS\COMMAND;c:\;c:\perl\bin;c:\98ddk\bin\win98;c:\progra~1\micros~4\vc98\bin\;c:\progra~1\micros~4\common\msdev98\bin;c:\progra~1\micros~4\vc98\include;c:\progra~1\micros~4\vc98\crt\src
There are at least two ways that this path provides access to:
stdio.h
so I am at a loss as to why this error is happening.
As a remedy,
I have tried to use vcvar32.bat
to establish the vc environment for batch commands
BUT it gets:
out of environment space
From another suggestion from days gone by,
I have put:
SHELL=C:\DOS\command.com c:DOS /P /E:512
into the config.sys file
and then rebooted,
BUT its still does not work.
(I don't have a DOS directory. so this had little hope of working,
right ?)
Does anyone have a solution for this ?
I believe that I need to get the dlls into out32dll
in order to eventually get SSLeay.pm to work in my Perl environment
in order to be able to open a
https
website, from a perl program,
and check this https website for its contents,
and (eventually) put a couple of things into the form that comes up
there,
and go on.
If I knew of something that I could BUY
that would solve this problem pronto
I might really consider buying it.
Does anybody know a person with such a WORKING method ?
HELP !
Mike
------------------------------
Date: Wed, 11 Jul 2001 18:18:15 +0000
From: gnari <gnarinn@hotmail.com>
Subject: Re: passing quotes to the command line
Message-Id: <994875495.916011685971171.gnarinn@hotmail.com>
In article <880a70aa.0107110514.7af2efd3@posting.google.com>,
Alan <slush@oceanfree.net> wrote:
>Sorry to bother you all,
>I am trying to pass a single quote properly to the command line and it
>does not seem to be working. I have tried escaping them and also using
>qq etc... i have to interpolate variables inside the quotes so
>unfortunatly I cannot use a single quote. Although that does do the
>job I need it to do but obviously I cannot use variables in there.
>
>example
>"dsh -w $node \'mount test_cws:/loglooker /mnt ; echo $?\'");
this looks strange with the ); at the end.
>I am trying to get it to pass back the $? from the remote machine.
>This command will work from the command line with the quotes set
>properly, but not when I pass it from within perl.
assuming that the command that works from the shell is
dsh -w $node 'mount test_cws:/loglooker /mnt ; echo $?'
and assuming that you want to use system to send it to the shell
you can do (in perl):
$command="dsh -w $node 'mount test_cws:/loglooker /mnt ; echo \$?'";
$res=system($command);
the key here is that you need to quote the $? or perl will expand it
>If anybody thinks that it could be a problem with the dsh command then
>that would also be appreciated.
>
I have no idea about that
gnari
------------------------------
Date: Wed, 11 Jul 2001 15:09:43 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: passing quotes to the command line
Message-Id: <3B4CA477.5293744F@earthlink.net>
Alan wrote:
>
> Sorry to bother you all,
> I am trying to pass a single quote properly to the command line and it
> does not seem to be working. I have tried escaping them and also using
> qq etc... i have to interpolate variables inside the quotes so
> unfortunatly I cannot use a single quote. Although that does do the
> job I need it to do but obviously I cannot use variables in there.
>
> example
> "dsh -w $node \'mount test_cws:/loglooker /mnt ; echo $?\'");
> I am trying to get it to pass back the $? from the remote machine.
> This command will work from the command line with the quotes set
> properly, but not when I pass it from within perl.
> If anybody thinks that it could be a problem with the dsh command then
> that would also be appreciated.
Try any of these:
system "dsh", "-w", $node, q{mount test_cws:/loglooker /mnt ; echo $?};
system qq{dsh -w $node "mount test_cws:/loglooker /mnt ; echo \$?"};
system "dsh -w $node ".'"mount test_cws:/loglooker /mnt ; echo $?"';
system "dsh -w $node \"mount test_cws:/loglooker /mnt ; echo \$?\"";
I reccommend the first one, unless you need to capture the output.
If that is the case, then:
$res = qx{dsh -w $node "mount test_cws:/loglooker /mnt ; echo \$?"};
die "dsh failed: $?, $!\n" if ($? or $!);
Or:
open DSH, qq{dsh -w $node "mount test_cws:/loglooker /mnt ; echo \$?"|}
or die "Couldn't fork: $!";
while( <DSH> ) {
# process lines
}
close DSH or die "dsh failed: $?, $!\n";
To learn about q, qq, qx see the perlop page in the quoting operators
section.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Wed, 11 Jul 2001 19:29:14 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: problem with data..
Message-Id: <eO137.1463$OS5.54880@news.pacbell.net>
Ilmari Karonen <iltzu@sci.invalid> wrote:
>>
>>The client does this...
>>
>> my $cmd = "tar cf - .";
>> print "$version\n";
>> print `$cmd`;
> I've no idea if this is relevant, but I'd rather write that as:
> $| = 1;
> print "$version\n";
> system "tar cf - ."; # maybe even exec, if that's the last line
Yup.
>>The server opens a filehandle by calling ssh to client with a pipe.
>>
>> my $cmd = "$ssh -e none -n ${user}\@${host} $remote_cmd 2>&1";
> Why 2>&1? Any errors from the client are going to mess up your input
> stream. (Hey, that could be it!) Just let STDERR go where it will.
To get the error message. If $? is set, I assume some error befell the
ssh command and I shove it into an error message.
> [snip]
>> warn "$0: read error occured: $!\n";
>> warn "$0: Skipping host $host.\n";
>> close (TAR);
>> close(SSH);
>> next HOST;
> Why not check the return value of close() here?
Because I already had an error, so I was going to throw away the client
run anyway.
> [snip]
>> close (SSH);
>> if ($?)
>> { warn "$0: Status from SSH close was $?: $!\n"; }
>> close(TAR);
>> if ($?)
>> { warn "$0: Status from TAR close was $?: $!\n"; }
> Here you are checking for errors, but I don't think you're doing it
> quite the way you're supposed to. Try this instead:
> close SSH or warn "$0: Status from SSH close was $?: $!\n";
> close TAR or warn "$0: Status from TAR close was $?: $!\n";
I see. I hadn't been aware that close would change it's return value
based on the pipe status. I went back and read that...
> Finally, just because I have it here, let me post my own perl-ssh-tar
> backup script. Note that this one only uses perl to set itself up, and
> then exec()s ssh to do the real work:
> #!/usr/bin/perl -w
> use strict;
> chdir '/home/iltzu/bak';
> open STDERR, '>>backup.log' or die $!;
> warn "--> starting at ". scalar(localtime). "\n";
> my ($year, $mon, $day) = (localtime)[5,4,3];
> $year += 1900; $mon++;
> my $file = sprintf("backup-%04d%02d%02d-incr.tgz", $year, $mon, $day);
> die "File $file exists!\n" if -e $file;
> open STDOUT, ">$file" or die "Opening '$file' failed: $!\n";
> exec qw(ssh -n -e none server.example.com),
> 'touch .last-backup-attempt '.
> '&& find www -type d -name "tmp" -prune '.
> ' -or -type f -newer .last-backed-up -print0 '.
> ' | tar -czf - -T - --null '.
> '&& mv .last-backup-attempt .last-backed-up'
> or die "exec failed: $!\n";
> __END__
> I've set up two cron jobs to run this, one every midnight and one at
> reboot. The result is that the backup gets done every night, or the
> next time the (desktop) system is turned on. Any further calls that
> day will see that the datestamped tarball exists and die immediately.
> Every once in a while I do a "find -ls" to see which files have been
> deleted, so that I won't have to bother restoring them when the shit
> really hits the fan.
In my particular case, I seem to have hit a bad ssh. I was using
ssh-1.2.27 and it simply corrupts things from time to time.
When I replaced my code with the shell script..
foreach $i (1 .. 20)
ssh host "/bin/cat /tmp/filename" > file.$i
and I got different filesizes, I realized it wasn't perl or my attempt
at coding it. The problem only appeared with my flaky network and
filesizes in excess of 200k. Fix the network, shorten the file, or fix
ssh (or use scp) and the problem disappears.
Replacing it with 1.2.31 fixed it up. (please no comments on why we are
using 1.2.x rather than other alternatives... I know...)
Thanks for the comments on the perl though.
--
Darren Dunham ddunham@taos.com
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< How are you gentlemen!! Take off every '.SIG'!! >
------------------------------
Date: 11 Jul 2001 19:52:31 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: problem with data..
Message-Id: <994880735.24300@itz.pp.sci.fi>
In article <eO137.1463$OS5.54880@news.pacbell.net>, Darren Dunham wrote:
>Ilmari Karonen <iltzu@sci.invalid> wrote:
>>
>> Why 2>&1? Any errors from the client are going to mess up your input
>> stream. (Hey, that could be it!) Just let STDERR go where it will.
>
>To get the error message. If $? is set, I assume some error befell the
>ssh command and I shove it into an error message.
I still think that looks like a bug. You might want to try deliberately
warn()ing on the remote end and seeing if anything breaks.
Without 2>&1, ssh will just pass any remote errors to the local STDERR.
With it, they will be inserted in the middle of the SSH stream, causing
potential corruption of the tar data.
>In my particular case, I seem to have hit a bad ssh. I was using
>ssh-1.2.27 and it simply corrupts things from time to time.
[snip]
>Replacing it with 1.2.31 fixed it up. (please no comments on why we are
>using 1.2.x rather than other alternatives... I know...)
Well, at least the original problem got fixed, then.
--
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real! This is a discussion group, not a helpdesk. You post something,
we discuss its implications. If the discussion happens to answer a question
you've asked, that's incidental." -- nobull in comp.lang.perl.misc
------------------------------
Date: Wed, 11 Jul 2001 16:13:01 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: puzzled over fork() doing something different to book example
Message-Id: <3B4CB34D.C7737FC5@earthlink.net>
Mark Grimshaw wrote:
>
> Hi,
>
> I'm investigating fork() and have typed in an example of it from a
> book ("Perl Interactive 5 Course" by the Waite Group Press, p468 in
> case anyone has it).
>
> Here's the script:
[snip]
> According to the book (and it seems logical too), my parent process
> will execute first, then the child, then the parent, then the child
> etc. etc. etc. The book claims I will get something like:
>
> I'm the parent. Please yell at my child.
> I'm the child. Be nice to me!
> hi there
> Parent said: hi there
> goodbye
> Parent said: goodbye
>
> However, the result of running this script is:
>
> hexie 593% ./fork.pl
> I'm the child. Be nice to me!
>
> I'm the parent. Please yell at my child.
>
> hi there
> Parent said: hi there
>
> Parent said:
>
> goodbye
> Parent said: goodbye
>
> Parent said:
>
> (I type in 'hi there' and 'goodbye' at the command line.)
>
> Not only does the child half-execute first (i.e. its first print
> statement only), but the parent executes then the child does what it's
> supposed to do but then immediately half-executes what it's supposed
> to do again.
As others have said, the half-execute can be fixed by eliminating the -l
To adjust the execution order, you need to force the child to wait until
the parent says it can go.
#!/usr/local/bin/perl -w
use strict;
pipe( RD, WR );
select( (select WR, $|=1)[0] );
if( my $pid = fork ) {
close RD;
print "I'm the parent. Please yell at my child.\n";
print WR, "x"; # tell the child to begin now.
print WR while( <> );
} elsif( defined $pid ) {
close WR;
read RD, $_, 1; # block until the parent tells us to begin.
print ""\t\t\tI'm the child. Be nice to me!\n";
print "Parent said: $_" while( <RD> );
} else {
die "Couldn't fork: $!\n";
}
__END__
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Wed, 11 Jul 2001 19:56:22 +0100
From: "Ian Stevens" <I.Stevens@btinternet.com>
Subject: Side effect of > operator?
Message-Id: <9ii7d9$jvj$1@plutonium.btinternet.com>
Hi all,
This has got me stumped. I have a bit of code where a variable is tested and
this appears to have an unwanted side effect that causes my program to fail.
I would be grateful if someone could shed some light on the matter as,
although I've got a workaround, I'm concerned that I don't know perl as much
as I thought :-(
The program uses, CGI, DBI and mysql so this complicates things a bit, but
I'm pretty sure the relevant sections are included below:
The code:
my $amount = $query_CGI->param('amount');
if ($amount > 0) { # Yes this does do nothing!
}
$insert_sql="insert ... values (?)";
$sth = $dbh->prepare($insert_sql);
$sth->execute ($amount); # This fails with a sql syntax error when the
CGI environment variable amount is unset
But if I use a temporary variable for $amount it works:
my $amount = $query_CGI->param('amount');
my $temp_amount = $amount; # These are the *only* changed lines
if ($temp_amount > 0) { #
}
$insert_sql="insert ... values (?)";
$sth = $dbh->prepare($insert_sql);
$sth->execute ($amount); # This succeeds
TIA
Ian
-----------------------------------------
Transact on the web - www.akriga.com
-----------------------------------------
------------------------------
Date: Wed, 11 Jul 2001 19:33:43 GMT
From: "Quanyi Sun" <quanyi.sun@nokia.com>
Subject: Re: Side effect of > operator?
Message-Id: <rS137.13990$m17.307181@news2.nokia.com>
Hi:
Check my explanations bellow.
Quanyi Sun
Ian Stevens wrote in message <9ii7d9$jvj$1@plutonium.btinternet.com>...
>Hi all,
>
>This has got me stumped. I have a bit of code where a variable is tested
and
>this appears to have an unwanted side effect that causes my program to
fail.
>I would be grateful if someone could shed some light on the matter as,
>although I've got a workaround, I'm concerned that I don't know perl as
much
>as I thought :-(
>The program uses, CGI, DBI and mysql so this complicates things a bit, but
>I'm pretty sure the relevant sections are included below:
>
>The code:
>
>my $amount = $query_CGI->param('amount');
>if ($amount > 0) { # Yes this does do nothing!
>}
Change to if (defined $amount) {...}. Variable $amount
will be undef if the parameter is unset.
>$insert_sql="insert ... values (?)";
>$sth = $dbh->prepare($insert_sql);
>$sth->execute ($amount); # This fails with a sql syntax error when the
>CGI environment variable amount is unset
This is because of the same reason.
>But if I use a temporary variable for $amount it works:
>
>my $amount = $query_CGI->param('amount');
>my $temp_amount = $amount; # These are the *only* changed lines
You are trying to initialize a new variable, in which
case $temp_amount will be "". This doesnot cause
problem to mySQL because when you are trying to
execute a ""-string statement, "0E0" is returned.
This value is treated as zero by Perl and interpretted
as true. Thus no problem.
>if ($temp_amount > 0) { #
>}
>$insert_sql="insert ... values (?)";
>$sth = $dbh->prepare($insert_sql);
>$sth->execute ($amount); # This succeeds
>
>TIA
>
>Ian
>
>-----------------------------------------
>Transact on the web - www.akriga.com
>-----------------------------------------
>
>
>
>
>
>
------------------------------
Date: Wed, 11 Jul 2001 18:18:55 GMT
From: echang@netstorm.net (E.Chang)
Subject: Re: Simple pattern-matching problem
Message-Id: <Xns90DB9220A8echangnetstormnet@207.106.93.86>
echang@netstorm.net (E.Chang) wrote in
<Xns90DB87C62D70Dechangnetstormnet@207.106.93.86>:
>"David A. McMurrey" <hcexres@io.com> wrote in
><3B4C8819.CEB3C919@io.com>:
>
>> Picture this -- I have an array with elements like this:
>>
>> $liner[10] = "CarlT|category1|judgeset2|6/13/01";
>> $liner[11] = "SarahW|category1|judgeset2|6/13/01";
>> $liner[12] = "SarahW|category13|judgeset2|6/13/01";
>>
>> I want to select only those elements containing "category1"
>>
>> $category = "category1";
>> if ($liner =~ /$category/) . . . .
>>
>> How do I exclude things like category13 ?
Or you can leave $category = "category1"; and use
if ($liner =~ /$category\b/)
--
EBC
------------------------------
Date: Wed, 11 Jul 2001 18:25:09 GMT
From: echang@netstorm.net (E.Chang)
Subject: Re: Simple pattern-matching problem
Message-Id: <Xns90DB932F02E3Fechangnetstormnet@207.106.93.86>
"David A. McMurrey" <hcexres@io.com> wrote in
<3B4C92A5.8F228ECE@io.com>:
>Thanks, EBC and Laszlo, but this is what I have -- it prints out
>*all* the items! For both ways of handling the variable
>
>-- David
>
>> !/usr/local/bin/perl
>
>$liner[0] = "JoeS|category1|judgeset1|6/13/01";
>$liner[1] = "MaryB|category1|judgeset1|6/13/01";
>$liner[2] = "JohnC|category1|judgeset1|6/13/01";
>$liner[3] = "JoelC|category1|judgeset2|6/13/01";
>
>$liner[4] = "JillW|category2|judgeset1|6/13/01";
>$liner[5] = "HenryW|category2|judgeset1|6/13/01";
>
>$liner[6] = "SethY|category3|judgeset1|6/13/01";
>$liner[7] = "UrsulaX|category3|judgeset1|6/13/01";
>$liner[8] = "BrendaS|category3|judgeset1|6/13/01";
>$liner[9] = "MaryZ|category3|judgeset2|6/13/01";
>$liner[10] = "CarlT|category3|judgeset2|6/13/01";
>$liner[11] = "SarahW|category3|judgeset2|6/13/01";
>
>$liner[12] = "SarahW|category13|judgeset2|6/13/01";
>
>> $category = "|category1|";
>$category = "category1\|";
>
>> select only the $ category items
>foreach $liner (@liner)
>{
> if ($liner =~ /$category/)
> {
> print "$liner \n";
> }
>}
My goof - in my test version I had
$category = 'category1\|';
with single quotes, not double. That works. So does using the
$liner =~ /$category\b/
that I mentioned in another post.
--
EBC
------------------------------
Date: Wed, 11 Jul 2001 13:37:51 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Simple pattern-matching problem
Message-Id: <slrn9kp3nf.g7d.tadmc@tadmc26.august.net>
David A. McMurrey <hcexres@io.com> wrote:
>Thanks, EBC and Laszlo, but this is what I have -- it prints out *all* the
>items! For both ways of handling the variable
>#!/usr/local/bin/perl
#!/usr/local/bin/perl -w
use strict;
Take all the help you can get.
>$category = "category1\|";
^ ^
^ ^
Add a simple debugging statement:
warn "category is '$category'\n";
Does $category contain what you want it to contain? Probably not.
$category = 'category1\|'; # single quotes, as it should be
or
$category = "category1\\|"; # Yuck!
># select only the $ category items
>foreach $liner (@liner)
>{
> if ($liner =~ /$category/)
Or just quote the metacharacters:
if ($liner =~ /\Q$category/)
Better is probably to put the separator in the pattern rather than
in the interpolated variable:
$category = 'category1';
if ($liner =~ /$category\|/)
Best would be to make sure that you don't match: foocategory1|
if ($liner =~ /\|$category\|/) # unless category can be the 1st field
[snip backwards-time quoted text...]
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 11 Jul 2001 19:05:29 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Simple pattern-matching problem
Message-Id: <3B4CA3C0.3242BB2A@acm.org>
"David A. McMurrey" wrote:
>
> Picture this -- I have an array with elements like this:
>
> $liner[10] = "CarlT|category1|judgeset2|6/13/01";
> $liner[11] = "SarahW|category1|judgeset2|6/13/01";
> $liner[12] = "SarahW|category13|judgeset2|6/13/01";
>
> I want to select only those elements containing "category1"
>
> $category = "category1";
> if ($liner =~ /$category/) . . . .
>
> How do I exclude things like category13 ?
my $category = qr/\|category1\|/;
my @found = grep /$category/, @liner;
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 11 Jul 2001 14:35:47 -0700
From: "Dustin Lee" <leed@ucdavis.edu>
Subject: Socket programming problem
Message-Id: <9iigk6$b7b$1@woodrow.ucdavis.edu>
I've been working on some code to test web servers for conformance to the
various HTTP RFCs. My program sends ALOT of messages and some of them
pretty big (at least compared to a typical GET). The problem is that I did
all of my development and testing on Win2000 (testing against web servers on
various platforms - Win2000, Linux, ...) and it works the way I want. Now
I'm trying to get it to work else where and I'm having troubles. For
instance I moved the code to a linux box and found that after the request
sizes (total size of request including method line and headers) reached a
certain size I would have trouble making connections with the servers at
all.
I've been pulling my hair out over this for a while but can't seem to make
any progress diagnosing why this might be. I've stripped out some code that
seems
to capture the same problem. It's not too pretty but hopefully you'll get
the idea.
- Why didn't I just use LWP? I needed more control over how the
requests/responses were handled (parsed/assembled). I did borrow some of
the LWP code though....
- Summary of problem: Code behaves differently between Win2000 and Linux.
Sometime loses connection and other times gets "short writes". In general
works better when running from Win2000 than from Linux, but in either case
the
program works perfectly fine against any servers on "localhost". Is there
anything
obviously wrong I'm doing with the sockets? My goal is to make a client
that successfully communicates with no cut off communications.
thanks in advance for any help,
dustin
Here's the test code I've reduced my problem to.....
#######################################################################
#######################################################################
#######################################################################
# try to provoke the socket problem....
use IO::Select;
use IO::Socket::INET;
$host_name = "test.host.net";
$port = 80;
foreach $size (50000 .. 10000000) {
my $long_uri = "/" . ("q" x $size);
print "size = $size\n";
my $methodline = "GET $long_uri HTTP/1.0\n\r\n\r";
my $response = &request($host_name, $port, $methodline);
}
######################################################################
sub request {
my ($uri_host, $uri_port, $request_text) = @_;
my $timeout = 10;
my $size = 4096;
# print "entered request with $uri_host, $uri_port, $request_text";
my $n = 0;
my $socket = _new_socket($uri_host,$uri_port,$timeout);
unless ($socket) {
die "no socket available!" . $!;
}
my $sel = IO::Select->new($socket) if $timeout;
# Lame hack to try and be "gentle" to the socket - didn't help...
# while ($write_offset < length($request_text)) {
# print "write offset is: $write_offset\n";
# my $write_len = &min ($size, length($request_text) - $write_offset);
# last if $write_len == 0;
# print "write len will be $write_len\n";
# $total_write += $socket->syswrite($request_text,
# $write_len,
# $write_offset);
# $write_offset += $size;
# }
print "sending request size: " . length($request_text) . "\n";
$n += $socket->syswrite($request_text,length($request_text));
die $! unless defined($n);
die "lost connection!!!" . $! if (not $socket->connected);
die "short write" unless $n == length($request_text);
die "read time out!\n" if ($timeout && !$sel->can_read($timeout) );
sleep 1;
my $response_text = "";
$n = $socket->sysread($response_text, $size);
if ($n == 0 || $n == undef) {
# another hack - if read doesn't return anything...
print "trying to read again...[$response_text_bit]\n";
$socket->flush;
print "last error was: ". $socket->error . "\n";
$n = $socket->sysread($response_text_bit, $size);
$response_text = $response_text_bit;
}
die "OOPS, need to read more but already full [$n]" unless ($n<=$size);
# Hack to force it into loop.
$n = 1 if $n <= 0;
die "no longer connected" if not $socket->connected;
while ($n > 0) {
print "entered loop....\n" if ($DEBUG == 1);
die "no longer connected" if not $socket->connected;
if (!$sel->can_read($timeout)) {
sleep 1; # hack to wait for more of the message
}
if ($timeout && !$sel->can_read($timeout)) {
last;
}
my $response_text_bit = "";
$n = $socket->sysread($response_text_bit, $size);
$response_text .= $response_text_bit;
}
print "size of response = ". length($response_text) . "\n";
return $response_text;
}
sub _new_socket
{
my($host, $port, $timeout) = @_;
local($^W) = 0; # IO::Socket::INET can be noisy
# my $sock = IO::Socket::INET->new(PeerAddr => $host,
my $sock = IO::Socket::INET->new(PeerHost => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => $timeout,
# $self->_extra_sock_opts($host, $port),
);
unless ($sock) {
# IO::Socket::INET leaves additional error messages in $@
$@ =~ s/^.*?: //;
die "Can't connect to $host:$port ($@)";
return 0;
}
$sock;
}
------------------------------
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.
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 1288
***************************************