[28930] in Perl-Users-Digest
Perl-Users Digest, Issue: 174 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 27 00:29:38 2007
Date: Mon, 26 Feb 2007 21:29:30 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 26 Feb 2007 Volume: 11 Number: 174
Today's topics:
perl "alternating foreach" feature idea <erik@q32.com>
Re: perl "alternating foreach" feature idea <spamtrap@dot-app.org>
Re: perl "alternating foreach" feature idea <noreply@gunnar.cc>
Re: perl "alternating foreach" feature idea <wahab-mail@gmx.de>
Re: perl "alternating foreach" feature idea <mritty@gmail.com>
Re: perl "alternating foreach" feature idea <nobull67@gmail.com>
Re: perl "alternating foreach" feature idea <bik.mido@tiscalinet.it>
Re: perl "alternating foreach" feature idea <jurgenex@hotmail.com>
Re: perl "alternating foreach" feature idea <bik.mido@tiscalinet.it>
Re: perl "alternating foreach" feature idea <spamtrap@dot-app.org>
Re: perl "alternating foreach" feature idea <bik.mido@tiscalinet.it>
Re: perl "alternating foreach" feature idea <greg.ferguson@icrossing.com>
Perl Script to download files (Need Help) NZJAV05@gmail.com
Re: Perl Script to download files (Need Help) <josef.moellers@fujitsu-siemens.com>
Re: Perl Script to download files (Need Help) <wahab-mail@gmx.de>
Re: Perl Script to download files (Need Help) <jurgenex@hotmail.com>
Re: Perl Script to download files (Need Help) <john.swilting@wanadoo.fr>
Re: Perl Script to download files (Need Help) <joe@inwap.com>
Perl threads - capturing value returned from sub <ecarlson@vmware.com>
Re: Perl threads - capturing value returned from sub <1usa@llenroc.ude.invalid>
Re: Perl threads - capturing value returned from sub <ecarlson@vmware.com>
Re: Perl threads - capturing value returned from sub <ecarlson@vmware.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 25 Feb 2007 02:06:27 -0800
From: "Erik 2.0" <erik@q32.com>
Subject: perl "alternating foreach" feature idea
Message-Id: <1172397987.526164.108430@a75g2000cwd.googlegroups.com>
dear perl people,
in the spirit of both perl's brevity and it's "do what i mean" array
contexts, it would be an appropriate feature for foreach loops to
support an array of variables as the iterator as well as just a
scalar. this would cause perl to alternate across the array:
for example:
foreach ($key, $val) in (@pairs) {
print "$key=$val\n";
}
an odd number of values in @pairs would cause an extra iteration with
$val set to undef
this syntax for an "alternating foreach" seems quite elegant to me.
it would reduce the number of keystrokes i type by dozens per week.
counting this email, and assuming the feature gets implemented, i'd be
net positive within 8 months or so.
- love
erik
------------------------------
Date: Sun, 25 Feb 2007 05:25:08 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <m28xemttyz.fsf@local.wv-www.com>
"Erik 2.0" <erik@q32.com> writes:
> in the spirit of both perl's brevity and it's "do what i mean" array
> contexts, it would be an appropriate feature for foreach loops to
> support an array of variables as the iterator as well as just a
> scalar. this would cause perl to alternate across the array:
>
> for example:
>
> foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
> }
>
> an odd number of values in @pairs would cause an extra iteration with
> $val set to undef
Great idea! When you've implemented it, you should send your patch to p5p
to get it integrated with mainline Perl.
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Sun, 25 Feb 2007 14:26:39 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <54dh1cF20gv2mU1@mid.individual.net>
Erik 2.0 wrote:
> it would be an appropriate feature for foreach loops to
> support an array of variables as the iterator as well as just a
> scalar. this would cause perl to alternate across the array:
>
> for example:
>
> foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
> }
>
> an odd number of values in @pairs would cause an extra iteration with
> $val set to undef
What's wrong with:
while ( ($key, $val) = splice @pairs, 0, 2 )
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 25 Feb 2007 14:36:19 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <ers3nv$77o$1@mlucom4.urz.uni-halle.de>
Gunnar Hjalmarsson wrote:
> What's wrong with:
>
> while ( ($key, $val) = splice @pairs, 0, 2 )
1) its not 'for'
2) it kills @pairs
consider a somehow 'generalized' form:
use strict;
use warnings;
sub shift_1{my @r; push@r,[shift@_]while@_;@r}
sub shift_2{my @r; push@r,[shift@_,shift@_]while@_;@r}
sub shift_3{my @r; push@r,[shift@_,shift@_,shift@_]while@_;@r}
my @array =qw' 1 2 3 4 5 6 7 8 9 ';
for my $ar ( shift_3 @array ) {
print $_ for @$ar, "\n";
}
Regards
M.
------------------------------
Date: 25 Feb 2007 06:24:13 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <1172413453.303201.159880@j27g2000cwj.googlegroups.com>
On Feb 25, 5:06 am, "Erik 2.0" <e...@q32.com> wrote:
> dear perl people,
>
> in the spirit of both perl's brevity and it's "do what i mean" array
> contexts, it would be an appropriate feature for foreach loops to
> support an array of variables as the iterator as well as just a
> scalar. this would cause perl to alternate across the array:
>
> for example:
>
> foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
>
> }
>
> an odd number of values in @pairs would cause an extra iteration with
> $val set to undef
>
> this syntax for an "alternating foreach" seems quite elegant to me.
> it would reduce the number of keystrokes i type by dozens per week.
> counting this email, and assuming the feature gets implemented, i'd > be net positive within 8 months or so.
perl -MList::MoreUtils=natatime -le'
my @pairs = qw/1 a 2 b 3 c 4 d/;
my $it = natatime 2, @pairs;
while (my ($key, $val) = $it->()){
print "$key = $val";
}
'
1 = a
2 = b
3 = c
4 = d
Paul Lalli
------------------------------
Date: 25 Feb 2007 07:31:00 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <1172417460.446436.273650@p10g2000cwp.googlegroups.com>
On Feb 25, 10:25 am, Sherm Pendley <spamt...@dot-app.org> wrote:
> "Erik 2.0" <e...@q32.com> writes:
>
> > foreach ($key, $val) in (@pairs) {
> > print "$key=$val\n";
> > }
>
> > an odd number of values in @pairs would cause an extra iteration with
> > $val set to undef
>
> Great idea! When you've implemented it, you should send your patch to p5p
> to get it integrated with mainline Perl.
I think this sort of thing in the core language is bloat. IIRC this
particular bloat is in Perl6. IMHO this is a case where we should have
KISS.
------------------------------
Date: Sun, 25 Feb 2007 17:26:33 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <7fd3u2t05rgf8d5qoi5usm1uos2l5k2vas@4ax.com>
On 25 Feb 2007 02:06:27 -0800, "Erik 2.0" <erik@q32.com> wrote:
>for example:
>
>foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
>}
Perl 6 supports this semantic in a syntactically very similar way:
pugs> for qw/foo 1 bar 2/ -> $a, $b { say $a ~ $b }
foo1
bar2
undef
>this syntax for an "alternating foreach" seems quite elegant to me.
I wouldn't call it "alternating foreach", but yes: it is. However you
know that ($key, $val) loudly screams for HASH, don't you?
>it would reduce the number of keystrokes i type by dozens per week.
That's strange because I do see the usefulness of the construct but
fail to see it coming out just SO useful all the time. Actually I've
never felt a compelling need for it.
>counting this email, and assuming the feature gets implemented, i'd be
>net positive within 8 months or so.
I have difficulties parsing the last phrase, care to explain?
However, as you may have discovered by now, clpmisc is hardly a place
for discussing development *of* Perl, i.e. features it *could* have
-although we all are tempted to do so from time to time-, but rather
one about development *in* Perl, i.e. features it *has*.
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: Sun, 25 Feb 2007 16:26:42 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <6NiEh.23314$kr6.15722@trndny09>
Erik 2.0 wrote:
> dear perl people,
>
> in the spirit of both perl's brevity and it's "do what i mean" array
> contexts, it would be an appropriate feature for foreach loops to
> support an array of variables as the iterator as well as just a
> scalar. this would cause perl to alternate across the array:
>
> for example:
>
> foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
> }
This looks like a poor design of your data structure. If you sequence has
alternating elements of identifiers (aka keys) and values, then an AoA seems
to be a much more natural way to represent this data.
And iterating over an array that contains just the references to the data
pairs works beautifully with the existing Perl features as of today.
> this syntax for an "alternating foreach" seems quite elegant to me.
Well, but your data structure is anything but.
jue
------------------------------
Date: Sun, 25 Feb 2007 17:29:02 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <e6e3u2lkg91f99i6i5vj7g8gs3b13kdj48@4ax.com>
On Sun, 25 Feb 2007 05:25:08 -0500, Sherm Pendley
<spamtrap@dot-app.org> wrote:
>Great idea! When you've implemented it, you should send your patch to p5p
>to get it integrated with mainline Perl.
I doubt that that idea, with that syntax could ever make its way into
Perl 5, and I cannot think of an easy modification that would make it
fit into current Perl syntax for C<for>.
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: Sun, 25 Feb 2007 13:16:53 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <m2zm72rtka.fsf@local.wv-www.com>
Michele Dondi <bik.mido@tiscalinet.it> writes:
> On Sun, 25 Feb 2007 05:25:08 -0500, Sherm Pendley
> <spamtrap@dot-app.org> wrote:
>
>>Great idea! When you've implemented it, you should send your patch to p5p
>>to get it integrated with mainline Perl.
>
> I doubt that that idea, with that syntax could ever make its way into
> Perl 5, and I cannot think of an easy modification that would make it
> fit into current Perl syntax for C<for>.
What I was really getting at was that first, p5p is a better place to talk
about additions to Perl than clpm, and second that code speaks far louder
there than ideas.
Apparently dry sarcasm doesn't translate very well to ASCII... :-\
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
------------------------------
Date: Sun, 25 Feb 2007 19:58:09 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <gsm3u2haqj94t8tlenc962te7k1u7avl9n@4ax.com>
On Sun, 25 Feb 2007 13:16:53 -0500, Sherm Pendley
<spamtrap@dot-app.org> wrote:
>> I doubt that that idea, with that syntax could ever make its way into
>> Perl 5, and I cannot think of an easy modification that would make it
>> fit into current Perl syntax for C<for>.
>
>What I was really getting at was that first, p5p is a better place to talk
>about additions to Perl than clpm, and second that code speaks far louder
>there than ideas.
No, no, I understood perfectly well your intent. Only, the OP may have
been tempted to really do so, or whatever. So for sake of completeness
I "pretended" you were being serious.
>Apparently dry sarcasm doesn't translate very well to ASCII... :-\
Naaah, it *does* translate well enough. Most of times, that is.
Actually I wanted to include in my reply something along the lines of
"I know this is just a reminder that this is not the best place to..."
but I didn't out of the desire to stay concise, for one - generally I
plainly can't!
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: 26 Feb 2007 08:11:40 -0800
From: "gf" <greg.ferguson@icrossing.com>
Subject: Re: perl "alternating foreach" feature idea
Message-Id: <1172506300.151907.315720@m58g2000cwm.googlegroups.com>
On Feb 25, 3:06 am, "Erik 2.0" <e...@q32.com> wrote:
> foreach ($key, $val) in (@pairs) {
> print "$key=$val\n";
>
> }
[...]
> this syntax for an "alternating foreach" seems quite elegant to me.
> it would reduce the number of keystrokes i type by dozens per week.
> counting this email, and assuming the feature gets implemented, i'd be
> net positive within 8 months or so.
To me, "elegance" in programming means doing something powerful within
the constraints of whatever language I'm working in. Not in changing
the language.
Try thinking in Perl terms...
use strict;
use warnings;
my @a = qw( one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8
nine 9 ten 10 );
my ( $b, $c );
for ( my $i = 0 ; ( $b, $c ) = @a[ $i, $i + 1 ], $i < scalar(@a) ; $i
+= 2 )
{
print $b, ' => ', $c, "\n";
}
one => 1
two => 2
three => 3
four => 4
five => 5
six => 6
seven => 7
eight => 8
nine => 9
ten => 10
If keystrokes equate to programming efficiency then you might want to
look into an editor that understands abbreviations or templates or
macros. I'll suggest vim, but that's because it's my particular
poison.
------------------------------
Date: 26 Feb 2007 00:51:07 -0800
From: NZJAV05@gmail.com
Subject: Perl Script to download files (Need Help)
Message-Id: <1172479867.381610.53690@k78g2000cwa.googlegroups.com>
Hi, I am completely new to using perl and I was wondering if I could
take just 2 min of you time to help me here...
I haven't used a perl script before, but I was wanting to know if
there is a script out there that can download files from a url ? and
if so, where can I download it... ?
Last question, how do I initiate the script ?
Sorry about the noob questions, but yeh, I am stumped....
Cheers,
Adam
------------------------------
Date: Mon, 26 Feb 2007 10:24:05 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Perl Script to download files (Need Help)
Message-Id: <eru942$hj8$1@nntp.fujitsu-siemens.com>
NZJAV05@gmail.com wrote:
> Hi, I am completely new to using perl and I was wondering if I could
> take just 2 min of you time to help me here...
2min times several hundred people ...
> I haven't used a perl script before, but I was wanting to know if
> there is a script out there that can download files from a url ? and
> if so, where can I download it... ?
There are numerous scripts that can do that.
Why not write it yourself? It's not _that_ complicated, e.g. using=20
LWP::Simple, and you'll learn a lot trying. If you have problems getting =
it to run, post your attempts here and we'll help getting you on-track=20
again.
> Last question, how do I initiate the script ?
Depends on the script.
> Sorry about the noob questions, but yeh, I am stumped....
BTW There are a couple of stand-alone (non-Perl) programs that can be=20
used to download files, e.g. wget or curl. Any specific reason why it=20
has to be Perl? Maybe the assignment specified Perl as the=20
implementation language?
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
------------------------------
Date: Mon, 26 Feb 2007 10:35:27 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Perl Script to download files (Need Help)
Message-Id: <eru9v3$rhs$1@mlucom4.urz.uni-halle.de>
NZJAV05@gmail.com wrote:
> Hi, I am completely new to using perl and I was wondering if I could
> take just 2 min of you time to help me here...
>
> I haven't used a perl script before, but I was wanting to know if
> there is a script out there that can download files from a url ? and
> if so, where can I download it... ?
>
> Last question, how do I initiate the script ?
Thats not really a Perl question, a simple
invocation of 'wget' (Unix/Linux/Windows)
will help a lot:
$> wget --referer="http : //www.moqawama.org/" \
-U "IE/6" \
https : //www.cia.gov/careers/jobs/operations_off_special.html
The above will download said page (after removing spaces).
Regards
M.
------------------------------
Date: Mon, 26 Feb 2007 14:40:13 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Perl Script to download files (Need Help)
Message-Id: <hjCEh.1571$RN6.1184@trndny07>
NZJAV05@gmail.com wrote:
> I haven't used a perl script before, but I was wanting to know if
> there is a script out there that can download files from a url ? and
> if so, where can I download it... ?
Your Question is Asked Frequently.
perldoc -q HTML:
"How do I fetch an HTML file?"
> Last question, how do I initiate the script ?
You execute it? Just like any other program or script written in any other
programming language.
jue
------------------------------
Date: Mon, 26 Feb 2007 18:59:20 +0100
From: "john.swilting" <john.swilting@wanadoo.fr>
Subject: Re: Perl Script to download files (Need Help)
Message-Id: <45e31ff7$0$27400$ba4acef3@news.orange.fr>
Josef Moellers wrote:
> NZJAV05@gmail.com wrote:
>> Hi, I am completely new to using perl and I was wondering if I could
>> take just 2 min of you time to help me here...
>
> 2min times several hundred people ...
>
>> I haven't used a perl script before, but I was wanting to know if
>> there is a script out there that can download files from a url ? and
>> if so, where can I download it... ?
>
> There are numerous scripts that can do that.
> Why not write it yourself? It's not _that_ complicated, e.g. using
> LWP::Simple, and you'll learn a lot trying. If you have problems getting
> it to run, post your attempts here and we'll help getting you on-track
> again.
>
>> Last question, how do I initiate the script ?
>
> Depends on the script.
>
>> Sorry about the noob questions, but yeh, I am stumped....
>
> BTW There are a couple of stand-alone (non-Perl) programs that can be
> used to download files, e.g. wget or curl. Any specific reason why it
> has to be Perl? Maybe the assignment specified Perl as the
> implementation language?
>
lwp-get
libnet
------------------------------
Date: Mon, 26 Feb 2007 12:58:44 -0800
From: Joe Smith <joe@inwap.com>
Subject: Re: Perl Script to download files (Need Help)
Message-Id: <Me-dnZWELe2Q137YnZ2dnUVZ_uejnZ2d@comcast.com>
NZJAV05@gmail.com wrote:
> I haven't used a perl script before, but I was wanting to know if
> there is a script out there that can download files from a url ? and
> if so, where can I download it... ?
Yes, several come with the perl installation.
> Last question, how do I initiate the script ?
Linux install: Perl is part of most distributions; installed already.
Linux command: bash% GET 'http://www.example.com/x/foo.cgi?bar=yes'
Windows install: http://www.activestate.com or http://www.cygwin.com
MS-DOS command window: C:\> GET "http://www.example.com/x/foo.cgi?bar=yes"
cygwin command: same as Linux
-Joe
------------------------------
Date: 26 Feb 2007 17:03:21 -0800
From: "Eric" <ecarlson@vmware.com>
Subject: Perl threads - capturing value returned from sub
Message-Id: <1172538201.694433.133140@j27g2000cwj.googlegroups.com>
Hello,
I am using Perl threads to launch multiple executions of a command in
parallel. The code to do this is:
foreach my $Machine ($self->Machines) {
my $thr = threads->new(\&doPowerAction, $Machine);
}
foreach my $t (threads->list()) {
$t->join;
}
The sub referenced is:
sub doPowerAction
{
my $machine = shift;
my $runScript = `power.exp $machine`;
if ($runScript =~ m/SUCCESS/) {
return "SUCCESS";
} elsif ($runScript =~ m/FAILURE/) {
return "FAILURE";
} else {
return "INTERNAL_ERROR";
}
}
The problem is that I don't seem to be able to figure out how to
capture the return value in the calling routine. Does anyone know how
to do this?
Thanks in advance to all that respond.
Eric
------------------------------
Date: Tue, 27 Feb 2007 01:21:36 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl threads - capturing value returned from sub
Message-Id: <Xns98E3CF1D4D1DBasu1cornelledu@127.0.0.1>
"Eric" <ecarlson@vmware.com> wrote in news:1172538201.694433.133140
@j27g2000cwj.googlegroups.com:
> I am using Perl threads to launch multiple executions of a command in
> parallel. The code to do this is:
Please read the posting guidelines for this group. Code you post should
be readily runnable by copying and pasting.
... code snipped ... edited version below
> The problem is that I don't seem to be able to figure out how to
> capture the return value in the calling routine. Does anyone know how
> to do this?
You need to store the returned values in a shared data structure.
I had to write and auxillary script to simulate the external
application.
#!/usr/bin/perl
use strict;
use warnings;
my @retvals = qw( SUCCESS FAILURE SOMETHING_ELSE );
print $retvals[ @retvals * rand ], "\n";
__END__
Then, replace your script with:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
my %status;
share %status;
my @machines = qw( apple orange banana pineapple cherry );
for my $machine ( @machines ) {
my $t = threads->new( \&action, $machine );
$t->join;
}
use Data::Dumper;
print Dumper \%status;
sub action {
my ($machine) = @_;
my $output = `s.pl`;
if ( $output =~ /SUCCESS/ ) {
$status{ $machine } = 'SUCCESS';
}
elsif ( $output =~ /FAILURE/ ) {
$status{ $machine } = 'FAILURE';
}
else {
$status{ $machine } = 'INTERNAL ERROR';
}
return;
}
__END__
C:\DOCUME~1\asu1\LOCALS~1\Temp\2> r
$VAR1 = {
'cherry' => 'SUCCESS',
'banana' => 'SUCCESS',
'apple' => 'FAILURE',
'orange' => 'INTERNAL ERROR',
'pineapple' => 'FAILURE'
};
--
Sinan
------------------------------
Date: 26 Feb 2007 17:24:40 -0800
From: "Eric" <ecarlson@vmware.com>
Subject: Re: Perl threads - capturing value returned from sub
Message-Id: <1172539480.386071.90030@s48g2000cws.googlegroups.com>
On Feb 26, 5:03 pm, "Eric" <ecarl...@vmware.com> wrote:
> Hello,
>
> I am using Perl threads to launch multiple executions of a command in
> parallel. The code to do this is:
>
> foreach my $Machine ($self->Machines) {
> my $thr = threads->new(\&doPowerAction, $Machine);
> }
> foreach my $t (threads->list()) {
> $t->join;
> }
>
> The sub referenced is:
>
> sub doPowerAction
> {
> my $machine = shift;
>
> my $runScript = `power.exp $machine`;
>
> if ($runScript =~ m/SUCCESS/) {
> return "SUCCESS";
> } elsif ($runScript =~ m/FAILURE/) {
> return "FAILURE";
> } else {
> return "INTERNAL_ERROR";
> }
>
> }
>
> The problem is that I don't seem to be able to figure out how to
> capture the return value in the calling routine. Does anyone know how
> to do this?
>
> Thanks in advance to all that respond.
>
> Eric
Cancel this one; I found the answer, which was to just assign a
variable to the join:
my $result = $t->join;
Seems like I always have a breakthrough immediately after I post to
this site! :)
Eric
------------------------------
Date: 26 Feb 2007 17:27:36 -0800
From: "Eric" <ecarlson@vmware.com>
Subject: Re: Perl threads - capturing value returned from sub
Message-Id: <1172539656.880984.93700@t69g2000cwt.googlegroups.com>
On Feb 26, 5:21 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> "Eric" <ecarl...@vmware.com> wrote in news:1172538201.694433.133140
> @j27g2000cwj.googlegroups.com:
>
> > I am using Perl threads to launch multiple executions of a command in
> > parallel. The code to do this is:
>
> Please read the posting guidelines for this group. Code you post should
> be readily runnable by copying and pasting.
>
> ... code snipped ... edited version below
>
> > The problem is that I don't seem to be able to figure out how to
> > capture the return value in the calling routine. Does anyone know how
> > to do this?
>
> You need to store the returned values in a shared data structure.
>
> I had to write and auxillary script to simulate the external
> application.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @retvals = qw( SUCCESS FAILURE SOMETHING_ELSE );
> print $retvals[ @retvals * rand ], "\n";
>
> __END__
>
> Then, replace your script with:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use threads;
> use threads::shared;
>
> my %status;
> share %status;
>
> my @machines = qw( apple orange banana pineapple cherry );
>
> for my $machine ( @machines ) {
> my $t = threads->new( \&action, $machine );
> $t->join;
>
> }
>
> use Data::Dumper;
> print Dumper \%status;
>
> sub action {
> my ($machine) = @_;
>
> my $output = `s.pl`;
> if ( $output =~ /SUCCESS/ ) {
> $status{ $machine } = 'SUCCESS';
> }
> elsif ( $output =~ /FAILURE/ ) {
> $status{ $machine } = 'FAILURE';
> }
> else {
> $status{ $machine } = 'INTERNAL ERROR';
> }
>
> return;
>
> }
>
> __END__
>
> C:\DOCUME~1\asu1\LOCALS~1\Temp\2> r
> $VAR1 = {
> 'cherry' => 'SUCCESS',
> 'banana' => 'SUCCESS',
> 'apple' => 'FAILURE',
> 'orange' => 'INTERNAL ERROR',
> 'pineapple' => 'FAILURE'
> };
>
> --
> Sinan
Thanks for your response, Sinan. I'll give it a try.
I didn't want to paste the entire code as I thought it would only add
clutter.
Eric
------------------------------
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 V11 Issue 174
**************************************