[Cialug] perl problem

Renegade Muskrat dramaley at spatulacity.cx
Wed Feb 8 17:58:16 CST 2006


I have a question regarding Perl. I am having trouble figuring out the
variable scoping in a script. I've reduced the problem to as simple of
a code snippet as i can in order to demonstrate it. In the script below 

i believe $test1 and $test2 should be the same. But they are not. Any
ideas why not? And how to make both of them display the local 
variables? Or a better place to ask Perl questions?


The script:


#!/usr/bin/perl
use strict;
use warnings;

our $foo = "global foo";

{
     my $foo = "local foo";
     my $bar = "local bar";

     no strict 'refs';
     my $test1 = join("\n", map { "$_ => ${$_}"; } qw(foo bar));
     my $test2 = join("\n", "foo => $foo", "bar => $bar");

     print "$test1\n----------\n$test2\n";
}


The output:


Use of uninitialized value in concatenation (.) or string at
./scratch line 12.
foo => global foo
bar =>
----------
foo => local foo
bar => local bar



More information about the Cialug mailing list