in reply to Re^3: foreach $1 in thread foreach $1
Yes, $a and $b seem to be OK. $" also seems to work, but $\ doesn't:
for $\ (qw( a b c d )) {
print "@{[1, 2, 3]}";
}
# Output:
# 1 2 31 2 31 2 31 2 3
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^5: foreach $1 -- all special variables
by Discipulus (Canon) on Jun 07, 2020 at 19:36 UTC
|
Hello choroba,
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:
use strict;
use warnings;
while (<DATA>){
my $var = (split)[0];
{
my $eval = "for $var ('OK '){ print $var }";
local $@;
eval $eval;
print "$eval\n";
if ( $@ ){
print "\nEVAL ERROR: $@\n";
}
}
}
# data (with some modifications) from https://www.perlmonks.org/?node_
+id=353259
__DATA__
$NOT_AVAILABLE # A NON EXISTING SPECIAL VARIABLE
$_ The default or implicit variable.
$a sort comparison routine variables.
$b sort comparison routine variables.
$1 Regexp parenthetical capture holders.
$2 Regexp parenthetical capture holders.
$31 # JUST TO EXAGERATE
$& Last successful match (degrades performance).
${^MATCH} Similar to $& without performance penalty. Requires /p mo
+difier.
$` Prematch for last successful match string (degrades performance)
+.
${^PREMATCH} Similar to $` without performance penalty. Requires /p
+ modifier.
$' Postmatch for last successful match string (degrades performance
+).
${^POSTMATCH} Similar to $' without performance penalty. Requires /
+p modifier.
$+ Last paren match.
$^N Last closed paren match (last submatch).
$^R Last regexp (?{code}) result.
${^RE_DEBUG_FLAGS} Current value of regexp debugging flags. See use
+ re 'debug';
${^RE_TRIE_MAXBUF} Control memory allocations for RE optimizations
+for large alternations.
${^ENCODING} The object reference to the Encode object, used to con
+vert the source code to Unicode.
${^OPEN} Internal use: \0 separated Input / Output layer informatio
+n.
${^UNICODE} Read-only Unicode settings.
${^UTF8CACHE} State of the internal UTF-8 offset caching code.
${^UTF8LOCALE} Indicates whether UTF8 locale was detected at startu
+p.
$. Current line number (or record number) of most recent filehandle
+.
$/ Input record separator.
$| Output autoflush. 1=autoflush, 0=default. Applies to currently s
+elected handle.
$, Output field separator (lists)
$\ Output record separator.
$" Output list separator. (interpolated lists)
$; Subscript separator. (Use a real multidimensional array instead.
+)
$% Page number for currently selected output channel.
$= Current page length.
$- Number of lines left on page.
$~ Format name.
$^ Name of top-of-page format.
$: Format line break characters
$^L Form feed (default "\f").
$^A Format Accumulator
$? Child error. Status code of most recent system call or pipe.
$! Operating System Error. (What just went 'bang'?)
$^E Extended Operating System Error (Extra error explanation).
${^CHILD_ERROR_NATIVE} Native status returned by the last pipe clos
+e, backtick (`` ) command, successful call to wait() or waitpid(), or
+ from the system() operator.
$$ Process ID
$< Real user id of process.
$> Effective user id of process.
$( Real group id of process.
$) Effective group id of process.
$0 Program name.
$^O Operating System name.
$] Old: Version and patch number of perl interpreter. Deprecated.
$^C Current value of flag associated with -c switch.
$^D Current value of debugging flags
$^F Maximum system file descriptor.
$^I Value of the -i (inplace edit) switch.
$^M Emergency Memory pool.
$^P Internal variable for debugging support.
$^R Last regexp (?{code}) result.
$^S Exceptions being caught. (eval)
$^T Base time of program start.
$^V Perl version.
$^W Status of -w switch
${^WARNING_BITS} Current set of warning checks enabled by use warni
+ngs;
$^X Perl executable name.
${^GLOBAL_PHASE} Current phase of the Perl interpreter.
$^H Internal use only: Hook into Lexical Scoping.
${^TAINT} Taint mode read-only flag.
${^WIN32_SLOPPY_STAT} If true on Windows stat() won't try to open t
+he file.
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] [Watch: Dir/Any] [d/l] [select] |
|
> 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.
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
Re^5: foreach $1
by LanX (Saint) on Jun 07, 2020 at 02:19 UTC
|
| [reply] [Watch: Dir/Any] |
|
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.
$ perl -MDevel::Peek -e'
for my $sym (qw( _ a " / \ 1 )) {
warn "\$$sym\n";
Dump($$sym);
warn "\n";
}
'
$_
SV = NULL(0x0) at 0x7fffc959c850
REFCNT = 1
FLAGS = ()
$a
SV = NULL(0x0) at 0x7fffc95545e0
REFCNT = 1
FLAGS = ()
$"
SV = PV(0x7fffc9554d40) at 0x7fffc95796f0
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x7fffc957e030 " "\0
CUR = 1
LEN = 10
$/
SV = PVMG(0x7fffc95732b0) at 0x7fffc95544f0
REFCNT = 1
FLAGS = (GMG,SMG,POK,pPOK)
IV = 0
NV = 0
PV = 0x7fffc9574ba0 "\n"\0
CUR = 1
LEN = 10
MAGIC = 0x7fffc9574b40
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x7fffc95544d8
MG_LEN = 1
MG_PTR = 0x7fffc9574b80 "/"
$\
SV = PVMG(0x7fffc95b9a80) at 0x7fffc95797f8
REFCNT = 1
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x7fffc957aba0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x7fffc95797e0
MG_LEN = 1
MG_PTR = 0x7fffc958fb70 "\\"
$1
SV = PVMG(0x7fffc9573a00) at 0x7fffc9584420
REFCNT = 1
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x7fffc9591e50
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x7fffc9584408
MG_LEN = 1
- local $_ is special, though.
Update: Added mention that local $_ is special and that $1 is implicitly localized. Added code. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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.
$ perl -MDevel::Peek -e'Dump($\); for $\ (1) { Dump($\); }'
SV = PVMG(0x7fffc3c4baa0) at 0x7fffc3c15ab0
REFCNT = 1
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x7fffc3c3b9e0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x7fffc3c15a20
MG_LEN = 1
MG_PTR = 0x7fffc3c3c4f0 "\\"
SV = IV(0x7fffc3c15a40) at 0x7fffc3c15a50
REFCNT = 2
FLAGS = (IOK,READONLY,PROTECT,pIOK)
IV = 1
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
> $_, $a, $b and $" aren't special.
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?
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|