Home > Archive > PERL Miscellaneous > September 2005 > print @{1} versus print @{11}
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
print @{1} versus print @{11}
|
|
| fishfry 2005-09-26, 3:56 am |
| 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.
| |
| Tassilo v. Parseval 2005-09-26, 3:56 am |
| 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=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
| |
| fishfry 2005-09-26, 6:58 pm |
| In article <slrndjf7sk.ra.tassilo.von.parseval@localhost.localdomain>,
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote:
> 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.
| |
| Anno Siegel 2005-09-26, 6:58 pm |
| fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote in comp.lang.perl.misc:
> In article <slrndjf7sk.ra.tassilo.von.parseval@localhost.localdomain>,
> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote:
>
[...]
[color=darkred]
[...]
[color=darkred]
>
> 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.
| |
| xhoster@gmail.com 2005-09-26, 6:58 pm |
| fishfry <BLOCKSPAMfishfry@your-mailbox.com> 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
| |
| Tad McClellan 2005-09-26, 6:58 pm |
| xhoster@gmail.com <xhoster@gmail.com> wrote:
> fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote:
>
> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Joe Smith 2005-09-27, 7:00 pm |
| Tad McClellan wrote:
> xhoster@gmail.com <xhoster@gmail.com> wrote:
>
> Perhaps the author quoted it because he had $^I in mind, which
> not strictly all punctuation characters.
Well, I consider tab (^I) and backspace (^H) as punctuation characters.
perl -le 'print +(ord($_)<32?ord($_):$_)," = $::" for sort keys %::'
-Joe
|
|
|
|
|