[Cialug] perl problem

Tim Wilson tim_linux at wilson-home.com
Wed Feb 8 21:51:06 CST 2006


My guess is because of the map call using a block.  Since you have another
block, you're referencing the global vars instead of the local vars, because
the local vars were in a different block.  Just a guess.

On 2/8/06, Renegade Muskrat <dramaley at spatulacity.cx> wrote:
>
> 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
>
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>



--
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cialug.org/pipermail/cialug/attachments/20060208/fcb0e576/attachment.html


More information about the Cialug mailing list