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

Re^9: foreach $1 -- all special variables

by Discipulus (Canon)
on Jun 08, 2020 at 08:42 UTC ( [id://11117808]=note: print w/replies, xml ) Need Help??


in reply to Re^8: foreach $1 -- all special variables
in thread foreach $1

Hello again,

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 ;)

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-29 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found