[ale] perl question

Calvin Harrigan charriglists at bellsouth.net
Mon Jan 16 15:45:04 EST 2012


On 1/16/2012 2:45 PM, Geoffrey Myers wrote:
> Okay, so why is it that $foo is empty inside the sub bar() ??
>
>
> my $bar;
> my $foo;
>
>
> foreach $foo ("test", "another") {
>
>       $bar = $foo;
>       print "before: bar<$bar>  foo<$foo>\n";
>       bar();
> }
>
>
> sub bar
> {
>       print "in bar(): bar<$bar>  foo<$foo>\n";
> }
>


Not sure you can use a global as an iterator like that and get the 
expected result. I think the foreach localizes the scope of the variable 
$foo.  Instead try:

my $bar;
my $foo;


foreach $i ("test", "another") {

      $bar = $foo = $i;
      print "before: bar <$bar> foo <$foo>\n";
      bar();
}


sub bar
{
      print "in bar(): bar <$bar> foo <$foo>\n";
}

Not a perl programmer by trade so bear with me, just guessing.  You can 
also try using the topic variable $_ by eliminating the $i all together.

my $bar;
my $foo;


foreach $_ ("test", "another") {

      $bar = $foo = $_;
      print "before: bar <$bar> foo <$foo>\n";
      bar();
}


sub bar
{
      print "in bar(): bar <$bar> foo <$foo>\n";
}





More information about the Ale mailing list