print @{1} versus print @{11}

Discussion in 'Perl Misc' started by fishfry, Sep 26, 2005.

  1. fishfry

    fishfry Guest

    Can someone please explain this result?

    print @{1}

    compiles but doesn't print anything.

    @{11}

    throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
    use"

    This one's really got me going. Perl 5.8.7.
    fishfry, Sep 26, 2005
    #1
    1. Advertising

  2. Also sprach fishfry:

    > Can someone please explain this result?
    >
    > print @{1}
    >
    > compiles but doesn't print anything.
    >
    > @{11}
    >
    > throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
    > use"
    >
    > This one's really got me going. Perl 5.8.7.


    This is a bit obscure and probably due to the special nature of the
    digit variables ($1, $2...). You get an idea when you use B::Deparse to
    see that for perl those two things are treated differently in a subtle
    manner:

    ethan@ethan:~$ perl -MO=Deparse -e 'print @{1}'
    print @1;
    -e syntax OK
    ethan@ethan:~$ perl -MO=Deparse -e 'print @{11}'
    print @{11;};
    -e syntax OK

    @{1} is condensed into @1. Strictures don't warn on certain symbols that
    are always global and live in package main::. These are variables with
    digits and punctuation as name (so you are always allowed to use e.g.
    $`, @`, %` etc., even $²).

    @{11} however is @{11;} which is a symbolic reference. That means the
    block {...} is executed and whatever is returned is turned into a string
    and taken as the name of the variable. These (also called soft
    references) are disallowed when "strict 'refs'" are in effect.

    Having said that, this different treatment of @{1} and @{11} is a bug
    IMO.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Sep 26, 2005
    #2
    1. Advertising

  3. fishfry

    fishfry Guest

    In article <>,
    "Tassilo v. Parseval" <> wrote:

    > Also sprach fishfry:
    >
    > > Can someone please explain this result?
    > >
    > > print @{1}
    > >
    > > compiles but doesn't print anything.
    > >
    > > @{11}
    > >
    > > throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
    > > use"
    > >
    > > This one's really got me going. Perl 5.8.7.

    >
    > This is a bit obscure and probably due to the special nature of the
    > digit variables ($1, $2...). You get an idea when you use B::Deparse to
    > see that for perl those two things are treated differently in a subtle
    > manner:
    >
    > ethan@ethan:~$ perl -MO=Deparse -e 'print @{1}'
    > print @1;
    > -e syntax OK
    > ethan@ethan:~$ perl -MO=Deparse -e 'print @{11}'
    > print @{11;};
    > -e syntax OK
    >
    > @{1} is condensed into @1. Strictures don't warn on certain symbols that
    > are always global and live in package main::. These are variables with
    > digits and punctuation as name (so you are always allowed to use e.g.
    > $`, @`, %` etc., even $2).
    >
    > @{11} however is @{11;} which is a symbolic reference. That means the
    > block {...} is executed and whatever is returned is turned into a string
    > and taken as the name of the variable. These (also called soft
    > references) are disallowed when "strict 'refs'" are in effect.
    >
    > Having said that, this different treatment of @{1} and @{11} is a bug
    > IMO.
    >


    Thank you much. But this brings up more questions.

    * What exactly is @1? I know what $1 is, but what's @1?

    * In perldoc perlop, if you search for '@{' you find this gem:

    "Punctuation" arrays such as @+ are only interpolated if the name is
    enclosed in braces @{+}.


    Now, what on earth is a "punctuation" array and why is "punctuation" in
    quotes? Googling reveals that this cryptic comment in perlop is the only
    known use of the phrase "punctuation array."

    And what does @{+} mean? What does it do?

    These are not idle questions by the way ... I'm trying to unpack some
    obfuscated Perl.
    fishfry, Sep 26, 2005
    #3
  4. fishfry

    Paul Lalli Guest

    Re: print @{1} versus print @{11}

    fishfry wrote:

    > But this brings up more questions.
    >
    > * What exactly is @1? I know what $1 is, but what's @1?


    No idea. Never heard of it. Don't know why Perl thinks @{1} is
    special.

    > * In perldoc perlop, if you search for '@{' you find this gem:
    >
    > "Punctuation" arrays such as @+ are only interpolated if the name is
    > enclosed in braces @{+}.
    >
    >
    > Now, what on earth is a "punctuation" array


    I would assume it means "arrays whose names are comprised soley of
    punctuation characters", as opposed to arrays whose names are comprised
    of letters and numbers.

    > and why is "punctuation" in quotes?


    Probably the authors' way of admitting that that's a pretty bizarre way
    of identifying non-user-defined arrays.

    > Googling reveals that this cryptic comment in perlop is the only
    > known use of the phrase "punctuation array."
    >
    > And what does @{+} mean? What does it do?


    That information is available in
    perldoc perlvar
    @+ This array holds the offsets of the ends of the last
    successful submatches in the currently active
    dynamic scope. "$+[0]" is the offset into the
    string of the end of the entire match. This is the
    same value as what the "pos" function returns when
    called on the variable that was matched against.
    The nth element of this array holds the offset of
    the nth submatch, so "$+[1]" is the offset past
    where $1 ends, "$+[2]" the offset past where $2
    ends, and so on. You can use "$#+" to determine how
    many subgroups were in the last successful match.
    See the examples given for the "@-" variable.

    Paul Lalli
    Paul Lalli, Sep 26, 2005
    #4
  5. fishfry

    Anno Siegel Guest

    fishfry <> wrote in comp.lang.perl.misc:
    > In article <>,
    > "Tassilo v. Parseval" <> wrote:
    >
    > > Also sprach fishfry:
    > >
    > > > Can someone please explain this result?
    > > >
    > > > print @{1}
    > > >
    > > > compiles but doesn't print anything.
    > > >
    > > > @{11}
    > > >
    > > > throws "Can't use string ("11") as an ARRAY ref while "strict refs" in
    > > > use"


    [...]

    > > ethan@ethan:~$ perl -MO=Deparse -e 'print @{1}'
    > > print @1;
    > > -e syntax OK
    > > ethan@ethan:~$ perl -MO=Deparse -e 'print @{11}'
    > > print @{11;};
    > > -e syntax OK


    [...]

    > > Having said that, this different treatment of @{1} and @{11} is a bug
    > > IMO.
    > >

    >
    > Thank you much. But this brings up more questions.
    >
    > * What exactly is @1? I know what $1 is, but what's @1?


    The same package variable, essentially, just like %1, the file handle 1
    the subroutine 1 and some more. Packages (symbol tables) are organized
    so that there is only one name entry for all of these, so in some sense
    if one is defined so are the others.

    > * In perldoc perlop, if you search for '@{' you find this gem:
    >
    > "Punctuation" arrays such as @+ are only interpolated if the name is
    > enclosed in braces @{+}.
    >
    >
    > Now, what on earth is a "punctuation" array and why is "punctuation" in
    > quotes? Googling reveals that this cryptic comment in perlop is the only
    > known use of the phrase "punctuation array."


    It's the manual's way of saying "an array with punctuation characters in
    its name".

    > And what does @{+} mean? What does it do?


    The name of a perl variable can (always) be enclosed in {} if necessary
    for disambiguation. Its most common use is in string interpolation

    my $plural = "${thing}s";

    but it has other uses as the example shows.

    This syntax does not make the {} block braces, nor their content perl
    code. Otherwise we'd have a bareword plus a symref.

    > These are not idle questions by the way ... I'm trying to unpack some
    > obfuscated Perl.


    ....which, of course, is the exact opposite of an idle activity :)

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 26, 2005
    #5
  6. fishfry

    Guest

    fishfry <> wrote:
    >
    > Thank you much. But this brings up more questions.
    >
    > * What exactly is @1? I know what $1 is, but what's @1?


    @1 is the array which happens to have the same name as the scalar $1.

    $1 is special, while @1 is not special other than that it has the same
    name as $1, which has the side-effect that @1 will not trigger errors under
    strict.

    >
    > * In perldoc perlop, if you search for '@{' you find this gem:
    >
    > "Punctuation" arrays such as @+ are only interpolated if the name is
    > enclosed in braces @{+}.
    >
    > Now, what on earth is a "punctuation" array and why is "punctuation" in
    > quotes?


    A punctuation array is an array whose name is composed of punctuation. It
    is in quotes because the other of perlop is notifying you that he is either
    coining the term, or is using the term advisedly.

    > Googling reveals that this cryptic comment in perlop is the only
    > known use of the phrase "punctuation array."


    A punctuation array is an array which has punctuation name.

    perldoc perlvar:

    NAME
    perlvar - Perl predefined variables

    DESCRIPTION
    Predefined Names

    The following names have special meaning to Perl. Most punctuation
    names have reasonable mnemonics, or analogs in the shells.
    ....


    >
    > And what does @{+} mean? What does it do?


    @{+} is the way you get @+ to interpolate into double-quoted strings. @+
    is documented in perldoc perlvar.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Sep 26, 2005
    #6
  7. <> wrote:
    > fishfry <> wrote:


    >> * In perldoc perlop, if you search for '@{' you find this gem:
    >>
    >> "Punctuation" arrays such as @+ are only interpolated if the name is
    >> enclosed in braces @{+}.
    >>
    >> Now, what on earth is a "punctuation" array and why is "punctuation" in
    >> quotes?

    >
    > A punctuation array is an array whose name is composed of punctuation. It
    > is in quotes because the other of perlop is notifying you that he is either
    > coining the term, or is using the term advisedly.



    Perhaps the author quoted it because he had $^I in mind, which
    not strictly all punctuation characters.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,876
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,478
  3. Replies:
    2
    Views:
    462
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    240
    Christoffer Sawicki
    Sep 2, 2006
  5. Paul Butcher
    Replies:
    12
    Views:
    688
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page