Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by rsFalse (Chaplain)
on Oct 11, 2017 at 14:38 UTC ( [id://1201167]=note: print w/replies, xml ) Need Help??


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

Then documentation should say that, not only scalar variables with names '\', '1' are valid, but also these names are valid for all structures. Otherwise it is a bug.

One can use "$1[$2]$3[$4]$5" expecting no array (with impossible names according doc) interpretation.

Thank you for informative reply and expanding name of a node to better!

Replies are listed 'Best First'.
Re^3: $1[ (or "Does an array @1 exist in Perl ? - Yes!")
by davido (Cardinal) on Oct 11, 2017 at 15:52 UTC

    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.


    Dave

Re^3: $1[ (or "Does an array @1 exist in Perl ? - Yes!")
by hippo (Bishop) on Oct 11, 2017 at 15:14 UTC
    One can use "$1[$2]$3[$4]$5" expecting no array (with impossible names according doc) interpretation.

    No, the documentation does not say they are impossible at all:

    Perl variable names may also be a sequence of digits, a single punctuation character, or the two-character sequence: ^ (caret or CIRCUMFLEX ACCENT) followed by any one of the characters [][A-Z^_?\] . These names are all reserved for special uses by Perl; for example, the all-digits names are used to hold data captured by backreferences after a regular expression match.

    So @1, @2, @1066, etc. are all perfectly valid. They are just reserved for special uses by Perl.

      One can reserve @1 , but the parser should croak or at least warn as long as the reservation wasn't filled with meaning.

      I think %+ and %- are good examples of taking advantage of reserved variables after introducing named captures in regex. ( added in Perl v5.10.0)

      @1 has no special meaning and doesn't need to be declared under strict!

      As a side note, it's possible to use @a or %b without explicit declaration under strict ... that's really not optimal and really should be documented.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        By the way, today I've found that in Modern_Perl book, as an example is given @3 as incorrect name:
        These are invalid Perl identifiers: my $invalid name; # space is invalid my @3; # cannot start with number my %~flags; # symbols invalid in name
        (from 4th edition, page #13)
Re^3: $1[ (perldoc)
by LanX (Saint) on Oct 11, 2017 at 15:08 UTC
    > Then documentation should say that, not only scalar variables with names '\', '1' are valid, but also these names are valid for all structures.

    I agree.

    > Otherwise it is a bug.

    It's a feature! ;]

    Honestly there are many issues with these "open source" docs, and I don't know how to solve them.

    The usual response is "contribute", but the result of more authors is just more confusion.

    We are somehow at the limit of the possibilities of self managed groups. (This observation doesn't only apply for the docs)

    regarding special symbols
    though in this particular case I'd suggest adding a remark to the beginning of perlvar and/or a caveat section at the end, to limit the amount of confusion.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found