print @{1} versus print @{11}

F

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.
 
T

Tassilo v. Parseval

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
 
F

fishfry

Tassilo v. Parseval said:
Also sprach fishfry:


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.
 
P

Paul Lalli

fishfry said:
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
 
A

Anno Siegel

fishfry said:
Tassilo v. Parseval said:
Also sprach fishfry:
[...]
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
 
X

xhoster

fishfry said:
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
 
T

Tad McClellan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top