Hi Timothy, Thanks for your reply.
On Sun, 4 Mar 2012 10:52:04 +0100 timothy adigun <2teezp...@gmail.com> wrote: > Hi Manfred, > > On Sun, Mar 4, 2012 at 8:56 AM, Manfred Lotz <manfred.l...@arcor.de> > wrote: > > > Hi there, > > I've got a strange problem with $_. It seems I do not understand > > something which might be basic. Anyway, here is a minimal example. > > > > #! /usr/bin/perl > > > > use strict; > > use warnings; > > use 5.010; > > > > sub sub1 { > > my $cmd = shift; > > > > print "Sub3: Issuing [$cmd]\n"; > > open my $fh, '-|', "$cmd 2>&1" or die "open: $!"; > > while ( <$fh> ) { print; } > > close $fh; > > return $? >> 8; > > } > > > > sub sub2 { > > my $cmd = shift; > > > > say "\nSub1 $cmd"; > > return 1; > > } > > > > > > my @cmds = ( "uname -a", "date"); > > > > Please use a lexical variable for your foreach(s) loop: > > > > foreach ( @cmds ) { say sub1($_); }; > > foreach ( @cmds ) { say sub2($_); }; > > > > like this: > foreach my $value1 ( @cmds ) { say sub1($value1); }; > foreach my $value2 ( @cmds ) { say sub2($value2); }; > Yep, this does it nicely. I guess I just will avoid using $_ in foreach loops. Makes life safer. -- Manfred -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/