in reply to Re^8: foreach $1 -- all special variables
in thread foreach $1
In effect some special variable can be aliased and still works properly ( as $" for example ) and others cannot ( as $\ and $1 ):
use strict; use warnings; print $/.'aliasing output record separator. $\\'."\n"; for $\ (qw( a b )) { # this does not works print "\$\\ is ->$\<- but is not appended\n"; } print $/.'aliasing output list separator $"'."\n"; for $" (qw( a b )) { # $" is aliased to a then b,c.. # BUT is still the output list separator print "@{[1, 2, 3]}\n"; } print $/.'aliasing eval error $@'."\n"; for $@ (qw( a b )) { print "now \$@ is: $@\n"; # no need to localize $@ eval '2 / 0'; print "then \$@ is: $@"; } print $/.'aliasing first captured match $1'."\n"; for $1 (qw( a b )) { print "now \$1 is: $1\n"; 'XXX' =~ /(.)/; print "then \$1 is WRONG after a match: $1\n"; { local $1; if ('YYYY' =~ /(.)/){ print "then \$1 is wrong even if \$1 is localized: ".( +$1 ? $1 : 'UNDEF')."\n"; } } } my $text =<<EOT; first second SEP fourth SEP last EOT print $/.'input record separator $/'."\n"; for $/ (qw( \n SEP )) { print "now \$/ is: $/\n"; print "LINE:->$_<-\n" for split $/,$text; } #OUTPUT aliasing output record separator. $\ $\ is ->a<- but is not appended $\ is ->b<- but is not appended aliasing output list separator $" 1a2a3 1b2b3 aliasing eval error $@ now $@ is: a then $@ is: Illegal division by zero at (eval 1) line 1. now $@ is: b then $@ is: Illegal division by zero at (eval 2) line 1. aliasing first captured match $1 now $1 is: a then $1 is WRONG after a match: a then $1 is wrong even if $1 is localized: UNDEF now $1 is: b then $1 is WRONG after a match: b then $1 is wrong even if $1 is localized: UNDEF input record separator $/ now $/ is: \n LINE:->first<- LINE:->second<- LINE:->SEP<- LINE:->fourth<- LINE:->SEP<- LINE:->last<- now $/ is: SEP LINE:->first second <- LINE:-> fourth <- LINE:-> last <-
L*
PS anyway we all know is a stupid thing to do ;)
|
|---|