in reply to Strange behavior of "my" in "perl -d" and "perl -de"
The thing is that within "perl -d", each line you type interactively is effectively in it's own block. Try the following:
Notice how the first initialization of $x did not persist, since that $x was a lexical, and hence confined to the scope of that line. The second initialization of $x did persist, however, because this $x is in fact $main::x:DB<1> my $x = 1 DB<2> p $x DB<3> $x = 1 DB<4> p $x 1
DB<5> p $main::x 1
Update: One other consequence of the fact that every line you type lives in its own block is that global variables that are implicitly localized to the enclosing scope (i.e. regexp-matching variables like $&, $', $`, $1, $2, $3...) will not survive from one line you type to the next. For example
Therefore, if you want to be able to use one of these variables over several lines, assign it to a package global:DB<1> 'a' =~ /(.)/ DB<2> print $1 DB<3> 'a' =~ /(.)/ && print $1 a DB<4>
DB<5> 'a' =~ /(.)/ && $g = $1 DB<6> print $g a DB<7>
Update: Added some emphasis.
the lowliest monk
|
|---|