in reply to Re^3: foreach $1
in thread foreach $1
for $\ (qw( a b c d )) { print "@{[1, 2, 3]}"; } # Output: # 1 2 31 2 31 2 31 2 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: foreach $1 -- all special variables
by Discipulus (Canon) on Jun 07, 2020 at 19:36 UTC | |
I dont see or understand how this last example demonstrate that $\ cant be used this way. I see all special variables working in the very same way as expected: how many we have seen foreach $a... dont use $a pattern? All special vars have the same behaviour:
My personal opinion is that this behaviour is correct and no errors nor warnings should be raised: such vars are always present and are somthing like super globals (is for such reason we localize $@ if needed) and if someone doesnt know about special vars and is so fool to use them this way is not a perl problem. I like special variables and I will even add more to the list (to obtain switches passed to perl like -l ) 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. | [reply] [d/l] [select] |
by LanX (Saint) on Jun 07, 2020 at 19:43 UTC | |
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 | [reply] |
by Discipulus (Canon) on Jun 07, 2020 at 19:53 UTC | |
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. | [reply] [d/l] [select] |
by LanX (Saint) on Jun 07, 2020 at 20:01 UTC | |
by Discipulus (Canon) on Jun 08, 2020 at 08:42 UTC | |
|
Re^5: foreach $1
by LanX (Saint) on Jun 07, 2020 at 02:19 UTC | |
ehm ... that's surprising
Cheers Rolf | [reply] |
by ikegami (Patriarch) on Jun 08, 2020 at 22:12 UTC | |
Aside from the fact that Perl itself uses them, $_, $a, $b and $" aren't special.[1] They are just ordinary package variables. $/ and $\ are special: They are magical vars. I don't know why they are magical vars. $1 is special: It's a magical var. It's also implicitly localized.
Update: Added mention that local $_ is special and that $1 is implicitly localized. Added code. | [reply] [d/l] [select] |
by ikegami (Patriarch) on Jun 08, 2020 at 22:50 UTC | |
Oh, and why does that matter? Well, since $" isn't special, Perl actually uses $" when you interpolate an array in double-quotes. It doesn't matter that the loop made $" refer to a different SV. But it does matter for magical vars. Since the SV that replaces $\ doesn't have the magic that $\, the underlying variable that Perl actually uses (instead of $\) isn't updated when $\ is changed.
| [reply] [d/l] [select] |
by LanX (Saint) on Jun 08, 2020 at 23:40 UTC | |
Where do you get this definition of "special" from, they are listed under perlvar#SPECIAL-VARIABLES. The docs specify the terminology > $/ and $\ are special: They are magical vars. ... $1 is special: It's a magical var OK, so your point seems to be that "magical vars", i.e. variables with a MAGIC attribute fail to work as expected as loop variables. So you agree that at least those vars should raise a warning in this case?
Cheers Rolf | [reply] [d/l] |
by ikegami (Patriarch) on Jun 09, 2020 at 17:57 UTC | |
by LanX (Saint) on Jun 09, 2020 at 18:19 UTC | |