Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^3: $1[ (or "Does an array @1 exist in Perl ? - Yes!")

by davido (Cardinal)
on Oct 11, 2017 at 15:52 UTC ( #1201171=note: print w/replies, xml ) Need Help??

in reply to Re^2: $1[ (or "Does an array @1 exist in Perl ? - Yes!")
in thread $1[

The fact that $1 is a pre-declared variable, and that @1 is not does not mean that @1 could not have been a variable, and consequently, that $1[... should not be seen as the beginning of an indexing into the @1 array. It really probably ought to be a strict violation first, since @1 isn't special, but is an identifier, and subject to the rules of parsing that apply to sigil-preceded identifiers when interpolated into a regexp quote-like construct.

perl -E 'our @array = (1,2,3); *1=\@array; say for @1;' 1 2 3

Here we cheated a little, using the typeglob syntax to create an array by the name of @1. Well, really a symbol for an array named @1, which happens to be an alias to @array in the symbol table. And it is shown to work. With that in mind, there's no reason that m/something$1[.../ should not be seen as the beginning of the indexing into an array element for interpolation within a regexp. Disambiguation can be achieved by wrapping the symbol portion of $1 in curly braces, as in ${1}:

# $1 will be undef, so not relevant to the pattern. [0] will be a sing +le-character character class, so matches "0". perl -E 'our @array = (1,2,3); *1=\@array; say for @1; say "yes" if "0 +" =~ m/.?${1}[0]/;' 1 2 3 yes

Otherwise, this will be variable interpolation:

# $1[0] is seen as interpolation, placing the value of "1" into the re +gexp, which matches with the "1" in our target string. perl -E 'our @array = (1,2,3); *1=\@array; say for @1; say "yes" if "1 +" =~ m/.?$1[0]/;' 1 2 3 yes

The unfortunate part here is that strict appears to be special cased for the numeric-variable symbols rather than specifically for scalars.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2021-01-17 09:44 GMT
Find Nodes?
    Voting Booth?