[28072] in Perl-Users-Digest
Perl-Users Digest, Issue: 9436 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 7 18:06:51 2006
Date: Fri, 7 Jul 2006 15:05:03 -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 Fri, 7 Jul 2006 Volume: 10 Number: 9436
Today's topics:
Re: Get the reference to an array from a function... <mritty@gmail.com>
Re: Get the reference to an array from a function... <sherm@Sherm-Pendleys-Computer.local>
Re: Get the reference to an array from a function... <mritty@gmail.com>
Re: Getting Net::SFTP handle takes FOR-FLIPPIN-EVER usenet@DavidFilmer.com
Re: help with some capturing syntax <ih8spam@spamsux.org>
Re: help with some capturing syntax <mritty@gmail.com>
Re: help with some capturing syntax <mritty@gmail.com>
Re: help with some capturing syntax <ih8spam@spamsux.org>
Re: help with some capturing syntax <mritty@gmail.com>
Re: help with some capturing syntax <ih8spam@spamsux.org>
Re: help with some capturing syntax <mritty@gmail.com>
Re: help with some capturing syntax <someone@example.com>
How to force formatted date (month) language ? <ynleder@nspark.org>
kill the process <cljlk@hotmail.com>
Re: kill the process <mritty@gmail.com>
Re: LWP and LocalAddr <lehmannmapson@cnm.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 7 Jul 2006 11:28:11 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <1152296891.043045.114570@s13g2000cwa.googlegroups.com>
David Squire wrote:
> > 2. return @arr,
>
> In this case test() returns a *list*, which contains the elements that
> are stored in the array @arr, but it is not the same thing as @arr
That's not strictly true. test() returns @arr. @arr is evaluated in
whatever context test() was called in. If called in list context, @arr
is evaluated to be the list of items @arr contains. If called in
scalar context, @arr is evaluated to be the number of items @arr
contains. Example:
$ perl -e'
sub bar {
@foo = (qw/a b c/);
return @foo;
}
my $x = bar();
print "$x\n";
'
3
> > assigh \test() to $arr_ref
>
> Now you have a problem. test() returns a list. You apply \ to this list,
You've got it backwards. test() returned @arr. The \ that was in
front of test() imposed list context on the call to test(), causing
@arr to be called in list context. Assinging the results of test() to
a scalar causes the list that was returned to be evaluated in scalar
context, which means to return the last element of that list.
------------------------------
Date: Fri, 07 Jul 2006 14:57:32 -0400
From: Sherm Pendley <sherm@Sherm-Pendleys-Computer.local>
Subject: Re: Get the reference to an array from a function...
Message-Id: <m2lkr5nsyr.fsf@Sherm-Pendleys-Computer.local>
"Paul Lalli" <mritty@gmail.com> writes:
> David Squire wrote:
>> > 2. return @arr,
>>
>> In this case test() returns a *list*, which contains the elements that
>> are stored in the array @arr, but it is not the same thing as @arr
>
> That's not strictly true.
Actually, it is - ask any XS programmer. If you attempt to return an array
or hash from a sub, what perl pushes onto the stack is a list of SV*s, not
the AV* or HV* itself.
Have a look at "perldoc perlcall" for the gory details.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 7 Jul 2006 13:45:35 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Get the reference to an array from a function...
Message-Id: <1152305135.845459.190730@75g2000cwc.googlegroups.com>
Sherm Pendley wrote:
> "Paul Lalli" <mritty@gmail.com> writes:
>
> > David Squire wrote:
> >> > 2. return @arr,
> >>
> >> In this case test() returns a *list*, which contains the elements that
> >> are stored in the array @arr, but it is not the same thing as @arr
> >
> > That's not strictly true.
>
> Actually, it is - ask any XS programmer. If you attempt to return an array
> or hash from a sub, what perl pushes onto the stack is a list of SV*s, not
> the AV* or HV* itself.
Poor choice of response on my part. My point was intended to be that
simply saying "return @arr" does not guarantee that any of the items
contained in @arr are going to end up in the variable(s) that are
assigned to the subroutine call. If the call itself is in scalar
context, then @arr is evaluated in scalar context. I have not yet read
the documentation you pointed me to, so I can only assume that in this
case, the list returned from the subroutine is infact a list containing
one element - the size of @arr.
> Have a look at "perldoc perlcall" for the gory details.
Thanks for the pointer. I'll delve into it when I have more time (and
am feeling slightly masochistic.... )
Paul Lalli
------------------------------
Date: 7 Jul 2006 12:09:19 -0700
From: usenet@DavidFilmer.com
Subject: Re: Getting Net::SFTP handle takes FOR-FLIPPIN-EVER
Message-Id: <1152299359.896128.279020@m79g2000cwm.googlegroups.com>
rahed wrote:
> Try to install Math::BigInt::GMP if it's not already there. That helped
> when I met weird behaviour with long ssh authentication times.
THAT WAS THE PROBLEM!!!!! I owe you a beer!
FWIW, for the benefit of anyone with this same problem who may stumble
upon this thread... installing Math::BigInt::GMP on AIX 5.3 requires a
hack to Makefile.PL - namely that (for some bizarre reason) you must
change
'LIBS' => ['-lgmp'],
to
'LIBS' => ['-L/usr/local/lib -lgmp'],
For more info, see my post in comp.unix.aix (http://tinyurl.com/rtrkz)
where I described various hacks necessary to install several libraries
and Perl modules (including GPG and Pari stuff) on AIX. This particular
hack for Math::GMP (which, apparently, is also needed for
Math::BigInt::GMP) was described by Sisyphus in a thread found here:
http://tinyurl.com/cb299.
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: Fri, 7 Jul 2006 14:16:21 -0400
From: "Matt Williamson" <ih8spam@spamsux.org>
Subject: Re: help with some capturing syntax
Message-Id: <e8m830$f41$1@nntp.aioe.org>
I figured it out. If there is a more efficient way to code it though, I'm
open.
foreach my $line (@content){
if ($line =~ /((job started)|(job ended)|(job completion status))/i) {
my $label = $1;
$line =~ /:(.*)$/;
my $status = $1;
chomp $status;
for ($status) {
s/^\s+//;
s/\s+$//;
}
print $label, " : ",$status, "\n";
}
"Matt Williamson" <ih8spam@spamsux.org> wrote in message
news:e8m4gg$tqu$1@nntp.aioe.org...
> Given the following, is there an easy way to preface the print $status,
> "\n"; line with job started, job ended or job completion status? I've been
> reading about about capturing in the blue camel, but I can't figure out if
> or how to make it work.
>
> foreach my $line (@content){
> if ($line =~ /(?:job started|job ended|job completion status)/i) {
> $line =~ /:(.*)$/;
> my $status = $1;
> chomp $status;
> for ($status) {
> s/^\s+//;
> s/\s+$//;
> }
> print |insert the status that matched above| $status, "\n";
> }
> }
>
> TIA
>
> Matt
>
------------------------------
Date: 7 Jul 2006 11:21:48 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: help with some capturing syntax
Message-Id: <1152296508.725734.213620@b28g2000cwb.googlegroups.com>
Matt Williamson wrote:
> Given the following, is there an easy way to preface the print $status,
> "\n"; line with job started, job ended or job completion status? I've been
> reading about about capturing in the blue camel, but I can't figure out if
> or how to make it work.
>
> foreach my $line (@content){
> if ($line =~ /(?:job started|job ended|job completion status)/i) {
Adding the ?: above specifically makes this *not* capture. If you
wanted to capture them, why are you specifically telling perl *not* to
capture them? Capture it, and then assign a permanent variable to $1,
so you can later print it out:
if ($line =~ /(job started|job ended|job completion status)/i) {
my $job_type = $1;
> $line =~ /:(.*)$/;
> my $status = $1;
> chomp $status;
More succinctly written: chomp (my ($status) = $line =~ /:(.*)$/);
> for ($status) {
> s/^\s+//;
> s/\s+$//;
> }
what is the point of a loop that goes iterates only once? Are you just
trying to avoid writing "$status" twice instead of once? Does that
really make sense to you?
$status =~ s/^\s+//;
$status =~ s/\s+$//;
Of course, you could have equally well just not captured the whitespace
in your original match.
> print |insert the status that matched above| $status, "\n";
> }
> }
If I were to write this whole code, to do what I *think* you're trying
to accomplish, it would look something like:
foreach my $line (@content){
if ($line =~ /(job (?:started|ended|completion
status)):\s*(.*?)\s*$/i) {
my ($job_type, $status) = ($1, $2);
print "$job_type: $status\n";
}
}
Of course, without any sample input or output to go by, I'm only
guessing.
Paul Lalli
------------------------------
Date: 7 Jul 2006 11:23:06 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: help with some capturing syntax
Message-Id: <1152296586.384766.227260@m73g2000cwd.googlegroups.com>
Matt Williamson wrote:
> I figured it out. If there is a more efficient way to code it though, I'm
> open.
>
> foreach my $line (@content){
> if ($line =~ /((job started)|(job ended)|(job completion status))/i) {
What do you think those three inner parentheses are doing?
(see my previous post for a critique of the rest of the code)
Paul Lalli
------------------------------
Date: Fri, 7 Jul 2006 15:06:36 -0400
From: "Matt Williamson" <ih8spam@spamsux.org>
Subject: Re: help with some capturing syntax
Message-Id: <e8mb14$1cq$1@nntp.aioe.org>
>> for ($status) {
>> s/^\s+//;
>> s/\s+$//;
>> }
>
> what is the point of a loop that goes iterates only once? Are you just
> trying to avoid writing "$status" twice instead of once? Does that
> really make sense to you?
>
> $status =~ s/^\s+//;
> $status =~ s/\s+$//;
>
It's in perlfaq 4 that way. I'm quite new to this, so I can't really say
what does or doesn't make sense. <g>
<snip>
> If I were to write this whole code, to do what I *think* you're trying
> to accomplish, it would look something like:
>
> foreach my $line (@content){
> if ($line =~ /(job (?:started|ended|completion
> status)):\s*(.*?)\s*$/i) {
> my ($job_type, $status) = ($1, $2);
> print "$job_type: $status\n";
> }
> }
>
> Of course, without any sample input or output to go by, I'm only
> guessing.
This is much cleaner code and you've taught me a good bit by posting it.
Thanks!
------------------------------
Date: 7 Jul 2006 12:10:48 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: help with some capturing syntax
Message-Id: <1152299448.486653.129290@h48g2000cwc.googlegroups.com>
Matt Williamson wrote:
> Paul Lalli wrote:
> > Matt Wiliamson wrote:
> >> for ($status) {
> >> s/^\s+//;
> >> s/\s+$//;
> >> }
> >
> > what is the point of a loop that goes iterates only once? Are you just
> > trying to avoid writing "$status" twice instead of once? Does that
> > really make sense to you?
> >
> > $status =~ s/^\s+//;
> > $status =~ s/\s+$//;
> >
>
> It's in perlfaq 4 that way.
Where? The only thing I see that deals with s/\s+// is at
http://perldoc.perl.org/perlfaq4.html#How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f
which gives you those two s///'s, but certainly doesn't recommend
creating a one-iteartion loop...
> > foreach my $line (@content){
> > if ($line =~ /(job (?:started|ended|completion
> > status)):\s*(.*?)\s*$/i) {
> > my ($job_type, $status) = ($1, $2);
> > print "$job_type: $status\n";
> > }
> > }
> >
> > Of course, without any sample input or output to go by, I'm only
> > guessing.
>
> This is much cleaner code and you've taught me a good bit by posting it.
> Thanks!
Quite welcome.
Paul Lalli
------------------------------
Date: Fri, 7 Jul 2006 15:19:17 -0400
From: "Matt Williamson" <ih8spam@spamsux.org>
Subject: Re: help with some capturing syntax
Message-Id: <e8mbou$n4k$1@nntp.aioe.org>
> Where? The only thing I see that deals with s/\s+// is at
> http://perldoc.perl.org/perlfaq4.html#How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f
> which gives you those two s///'s, but certainly doesn't recommend
> creating a one-iteartion loop...
>
This is what it says in my version of Perlfaq 4
___
How do I strip blank space from the beginning/end of a string?
Although the simplest approach would seem to be
$string =~ s/^\s*(.*?)\s*$/$1/;
not only is this unnecessarily slow and destructive, it also fails with
embedded newlines. It is much faster to do this operation in two steps:
$string =~ s/^\s+//;
$string =~ s/\s+$//;
Or more nicely written as:
for ($string) {
s/^\s+//;
s/\s+$//;
}
This idiom takes advantage of the "foreach" loop's aliasing behavior to
factor out common code. You can do this on several strings at once, or
arrays, or even the values of a hash if you use a slice:
# trim whitespace in the scalar, the array,
# and all the values in the hash
foreach ($scalar, @array, @hash{keys %hash}) {
s/^\s+//;
s/\s+$//;
}
___
Since it said "Or more nicely written as" I thought that must be the correct
way to do it. It now seems that it's only better if you have multiple
strings to use it on.
Matt
------------------------------
Date: 7 Jul 2006 12:30:07 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: help with some capturing syntax
Message-Id: <1152300607.879927.106450@75g2000cwc.googlegroups.com>
Matt Williamson wrote:
> > Where? The only thing I see that deals with s/\s+// is at
> > http://perldoc.perl.org/perlfaq4.html#How-do-I-strip-blank-space-from-the-beginning%2fend-of-a-string%3f
> > which gives you those two s///'s, but certainly doesn't recommend
> > creating a one-iteartion loop...
> >
>
> This is what it says in my version of Perlfaq 4
>
> ___
>
> How do I strip blank space from the beginning/end of a string?
> Although the simplest approach would seem to be
>
> $string =~ s/^\s*(.*?)\s*$/$1/;
>
> not only is this unnecessarily slow and destructive, it also fails with
> embedded newlines. It is much faster to do this operation in two steps:
>
> $string =~ s/^\s+//;
> $string =~ s/\s+$//;
>
> Or more nicely written as:
>
> for ($string) {
> s/^\s+//;
> s/\s+$//;
> }
Yeesh. I certainly am glad that FAQ's been updated. :-)
>
> This idiom takes advantage of the "foreach" loop's aliasing behavior to
> factor out common code. You can do this on several strings at once, or
> arrays, or even the values of a hash if you use a slice:
>
> # trim whitespace in the scalar, the array,
> # and all the values in the hash
> foreach ($scalar, @array, @hash{keys %hash}) {
> s/^\s+//;
> s/\s+$//;
> }
>
> ___
>
> Since it said "Or more nicely written as" I thought that must be the correct
> way to do it.
One of the things you should learn about Perl is that there's no such
thing as "the" correct way to do anything. Indeed, one of Perl's
mottos is "There Is More Than One Way To Do It". Obviously, someone
thought (and probably still thinks) that aliasing the variable to $_ by
means of a one-iteration foreach was a good way of doing it. I
disagree, as it seems to be needlessly misleading. That doesn't mean
that either my way or that old FAQ's way are "wrong".
> It now seems that it's only better if you have multiple strings to use it on.
That is my opinion, yes. There is, of course, something to be said for
extensability. With the FAQ's way, your code is all set to have more
variables added to it, just by typing them into the foreach's list.
"My" way, you'd have to copy and paste code. You need to decide which
is the worst trade-off: readability, or extensability.
Paul Lalli
------------------------------
Date: Fri, 07 Jul 2006 20:19:53 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: help with some capturing syntax
Message-Id: <Jlzrg.104989$A8.63762@clgrps12>
Matt Williamson wrote:
> Given the following, is there an easy way to preface the print $status,
> "\n"; line with job started, job ended or job completion status? I've been
> reading about about capturing in the blue camel, but I can't figure out if
> or how to make it work.
>
> foreach my $line (@content){
> if ($line =~ /(?:job started|job ended|job completion status)/i) {
You are using non-capturing parentheses. If you want to capture the job
status you have to use capturing parentheses:
if ($line =~ /(job (?:started|ended|completion status))/i) {
my $job_status = $1;
> $line =~ /:(.*)$/;
> my $status = $1;
You should only use the numerical variables after a successful match:
my ( $status ) = $line =~ /:(.*)$/;
> chomp $status;
/:(.*)$/ will not match a newline so unless you have changed the value of $/
there is nothing for chomp to remove and in any case the s/\s+$//; later would
remove any trailing newlines.
> for ($status) {
> s/^\s+//;
> s/\s+$//;
> }
> print |insert the status that matched above| $status, "\n";
> }
> }
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 8 Jul 2006 00:02:45 +0200
From: Yohan N. Leder <ynleder@nspark.org>
Subject: How to force formatted date (month) language ?
Message-Id: <MPG.1f18f86c5159c22798987b@news.tiscali.fr>
Hi,
I would like to produce a formatted GMT date & time string which will
depend of a user choice in a web form (in a CGI framework) about human
language he prefers (e.g. a french native may prefer to choose
'english'). I wish that it works under both Unix and Windows.
Here is a test script on which I'm trying to go through POSIX::setlocale
() as described in 'perldoc perllocale' about LC_TIME :
#!/usr/bin/perl -w
use strict;
use POSIX qw(locale_h strftime);
print "Content-type: text/html\n\n";
my $datetime;
# english
setlocale(LC_TIME, "en_US.ISO8859-1");
$datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
print "<p>ENGLISH =>".$datetime."</p>";
# french
setlocale(LC_TIME, "fr_FR.ISO8859-1");
$datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
print "<p>FRENCH => ".$datetime."</p>";
exit 0;
But it doesn't seems to work because it just displays (in client
browser) the same french string in both cases (tested with ActivePerl
5.8.8 under Windows 2K FR) :
ENGLISH => 07 juil. 2006 @ 21:18:35 GMT
FRENCH => 07 juil. 2006 @ 21:18:35 GMT
When I would like to obtain something like :
ENGLISH => 07 Jul 2006 @ 21:18:35 GMT
FRENCH => 07 juil. 2006 @ 21:18:35 GMT
I've also tried with Perl 5.00503 under FreeBSD US, and, this time, it
gives english strings only (the only difference is the minus 'j' in
French string, but still with English word):
ENGLISH =>07 Jul 2006 @ 21:41:14 GMT
FRENCH => 07 jul 2006 @ 21:41:14 GMT
How to proceed to get this string in English or French (i.e. month here)
for sure (ie. depending of a language value extracted from STDIN about
web form POST in the real script) ?
------------------------------
Date: 7 Jul 2006 12:51:15 -0700
From: "blackdog" <cljlk@hotmail.com>
Subject: kill the process
Message-Id: <1152301875.172460.116150@m73g2000cwd.googlegroups.com>
I have a perl script, I like to kill it (commit suicide) if the script
is running on the system for more than one hour. What is the best way
to do it?
------------------------------
Date: 7 Jul 2006 13:00:19 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: kill the process
Message-Id: <1152302419.362404.75700@s53g2000cws.googlegroups.com>
blackdog wrote:
> I have a perl script, I like to kill it (commit suicide) if the script
> is running on the system for more than one hour. What is the best way
> to do it?
Are you saying you want the program to automatically exit if it's been
running for an hour? At the start of your program, get the current
time (see: perldoc -f time). Periodically in your code, presumably in
a main central loop of some kind, check the current time. If the two
times are more than 3600 seconds apart, exit (see: perldoc -f exit).
You may also wish to make use of the END { } block (see: perldoc
perlmod) for any cleanup code that you wish to happen.
If, on the other hand, you want some external process to periodically
check your Perl program to see if it's been running for an hour, then
your question is off topic for this group, as the question does not at
all depend on the language in which the hour-long script was written.
If, on a mysterious third hand, you want this external process which
periodically checks your Perl script to also be written in Perl, then
your question is back on topic here. You may wish to use the CPAN
module Proc::ProcessTable to determine how long your program has been
running for, and then send it a signal (see: perldoc -f kill). Your
hour-long script would have to handle that signal, of course. (see:
perldoc perlipc and perldoc perlvar (searching for %SIG)).
Once you've made your attempt to solve your problem, if you cannot get
satisfactory results, feel free to post what you've tried here. Be
sure to read and follow the Posting Guidelines for this group, which
are posted here twice a week, to maximize your chances of getting good
help.
Paul Lalli
------------------------------
Date: Fri, 07 Jul 2006 22:30:20 +0200
From: Marten Lehmann <lehmannmapson@cnm.de>
Subject: Re: LWP and LocalAddr
Message-Id: <4h7um8F1qjihnU1@individual.net>
Hello,
> Of course you can't (or you should not) spoof your source IP address
> ;-)
my intention was not to spoof an IP-address. I found another solution in
older newsgroup entries:
@LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => $my_ip );
Setting this, the server I'm connecting to sees $my_ip as the
remote-address. Of course I cannot locally bind to an IP-address on my
server that is not configured with ifconfig.
Regards
Marten
------------------------------
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 9436
***************************************