Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by LanX (Saint)
on Jun 07, 2020 at 19:43 UTC ( [id://11117786]=note: print w/replies, xml ) Need Help??


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

> I dont see or understand how this last example demonstrate that $\ cant be used this way.

Look at the output, a b c should show up as "line delimiters" when $\ is iterating thru but are ignored.

OTOH when setting $" it does effect the internal delimiter of the array interpolation.

It's hard to understand why iterating with one special var has an effect while the other doesn't. This is the opposite of orthogonal behavior.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^6: foreach $1 -- all special variables

Replies are listed 'Best First'.
Re^7: foreach $1 -- all special variables
by Discipulus (Canon) on Jun 07, 2020 at 19:53 UTC
    Hello LanX I probably miss the point.. but $\ becomes an alias of a b c d elements as in all previous cases, no?

    for $\ (qw( a b c d )) { print "@{[1, 2, 3]}"; } print $/; for $_ (qw( a b c d )) { print "@{[1, 2, 3]}"; } #output 1 2 31 2 31 2 31 2 3 1 2 31 2 31 2 31 2 3

    L*

    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.
      Please try $" not $_
      for $" (qw( a b c d )) { print "@{[1, 2, 3]}"; }

      I'm mobile so I can't show it myself

      Edit

      ... Wait, termux to the rescue :)

      $ perl for $" (qw( a b c d )) { print "@{[1, 2, 3]}"; } __END__ 1a2a31b2b31c2c31d2d3$

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        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://11117786]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found