Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print @{1} versus print @{11}

Reply
Thread Tools

print @{1} versus print @{11}

 
 
fishfry
Guest
Posts: n/a
 
      09-26-2005
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.
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-26-2005
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:eparse 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=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
 
 
 
fishfry
Guest
Posts: n/a
 
      09-26-2005
In article < ldomain>,
"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:eparse 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.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-26-2005
fishfry <> wrote in comp.lang.perl.misc:
> In article < ldomain>,
> "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.
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-26-2005
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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-26-2005
<> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
internet explorer print preview VERSUS print eunever32@yahoo.co.uk HTML 2 11-22-2006 02:03 PM
script versus code versus ? Russ ASP .Net 1 06-10-2004 03:06 AM
HTML Client Control versus. HTML Server Control versus. Web Server Control Matthew Louden ASP .Net 1 10-11-2003 07:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57