[ale] perl question

Geoffrey Myers lists at serioustechnology.com
Mon Jan 16 16:10:20 EST 2012


Calvin Harrigan wrote:
> 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.

Oh the loop works as defined, it's just that the value is not passed to 
the sub bar()  My expectation was that since the call to bar() was 
inside the loop, that $foo would be accessible within the sub.

> 
> 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";
> }
> 
> 
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo


-- 
Until later, Geoffrey

"I predict future happiness for America if they can prevent
the government from wasting the labors of the people under
the pretense of taking care of them."
- Thomas Jefferson


More information about the Ale mailing list