Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: variable has value but then become empty

by sundialsvc4 (Abbot)
on Sep 09, 2015 at 14:44 UTC ( [id://1141423]=note: print w/replies, xml ) Need Help??


in reply to variable has value but then become empty

Also consider the implications of the following bit of code, which compiles and runs without errors or warnings:

use strict; use warnings; my $foo = "bar "; { my $foo = "foo "; print $foo; } print $foo;
Whose output is:   foo bar

The definition of $foo which exists within the anonymous block conceals, without warning, the variable of the same name that exists outside the block.   Two entirely distinct values are maintained, each referred-to at different times and in different scopes by the same variable name.  Within the block, the value "bar " cannot be reached, and there is nothing to alert you to the probably-unintentional conflict.

This is not “incorrect” behavior.   It is, in fact, by design, although it might be slightly different from other languages you have used (and whose error, warning, and informational messages you might have come to rely upon).   This it just might be yet-another reason why “global variables are not a particularly good idea.”

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1141423]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found