Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found