dpmott has asked for the wisdom of the Perl Monks concerning the following question:
So, I was quickly coding up a proof-of-concept script today, and I fat-fingered a line that was supposed to check the $@ variable. However, the compiler didn't catch my use of the undocumented global array @$.
So I wrote some test code:
#!perl
use strict; # need to pre-declare variables...
@$ = (1, 2, 3, 4); # I wonder what this is?
eval { die "blah"; }; # This sets $@
print join(', ', @$); # prints '1, 2, 3, 4'
Could someone please tell me what the @$ array is, what it's used for, and/or why the compiler doesn't warn or die when I use it? It's not documented in perlvar or any reference material that I have. One of my co-workers speculated that the implementation probably reserved the glob of '$' variables, since there's the '$$' variable...
I don't mind if I get dyslexic with '$$', but '@$' should be some kind of compile-time error, IMHO.
Unless it's actually used for something? Anyone?
Thanks,
-dpmott
Re: Undocumented variable?
by Zaxo (Archbishop) on Sep 12, 2003 at 21:36 UTC
|
Punctuation variables are exempt from stricture and perfectly legal. AFAIK @$ is not a perl global or package variable, and you are free to use it.
Mostly seen in golf and obfu, for obvious reasons.
After Compline, Zaxo
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
ARGVOUT
This is a new one for me. What/how is that used?
Update: Never mind, found it. Though I see I already got a reply, thanks! (and downvotes for an honest question, some people are just too funny ;~) Still, was a new one to me... ah well.
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.
| [reply] [Watch: Dir/Any] |
|
|
| [reply] [Watch: Dir/Any] |
|
|
|
|
| [reply] [Watch: Dir/Any] |
|
"Perl Golf" is a game that monks and wizards like to play, wherein you try to come up with the solution to a problem in the fewest characters possible. It often starts from a very basic question, in order to break up the boredom of helping newbies. One can occasionally learn nifty tricks from golfing, but it's mostly just playing.
Obfu was also mentioned, which is short for obfuscation, or the writing of intentionally hard to read code. Perl's flexibility certainly makes it possible to write extremely obfuscated code (especially when done intentionally) - click on the obfuscation link, probably near the top of this very page, for some nifty examples.
-- Kirby
| [reply] [Watch: Dir/Any] |
Re: Undocumented variable?
by Mr. Muskrat (Canon) on Sep 12, 2003 at 21:39 UTC
|
The built-in variables documented in perlvar are a part of a declared typeglob. That means you can even do silly things like this:
#/usr/bin/perl
use strict;
use warnings;
@> = qw(Roses are red. Violets are blue.);
print $_, " " for @>;
@> looks like a wrapped rose to me... | [reply] [Watch: Dir/Any] [d/l] |
|
You can even make Perl program that looks like a rose with
a stem. The following program compiles and runs, although it
doesn't do anything useful:
perl -e '@}-`-,-`-%-'
Abigail | [reply] [Watch: Dir/Any] [d/l] |
Re: Undocumented variable?
by jonadab (Parson) on Sep 13, 2003 at 04:08 UTC
|
As the other poster says, all punctuation has a certain
amount of magic. Even if a given punctuation variable
doesn't have any specific magic, it still has the general
magic that applies to all of them. If you read the
obfuscation section from time to time, you'll see people
using punctuation characters to name not just scalars
and arrays but also hashes. Certain punctuation
characters are particularly popular for this, most
notably underscore and semicolon. (%; makes for some
good fun, particularly. $_{$_} is also interesting in
the right combinations.) _ is also a legal name for
a subroutine, though I'm not sure there's any magic in
that case.
I believe the reason all punctuation variables are
automatically magical is a combination of
consistency and implementation details, but frankly
it shouldn't be a problem in most cases, because if
you don't want weird stuff to happen you shouldn't
use weird variables; there are plenty of letters in
the alphabet, after all, if you're a fan of the mundane.
In Perl 6, some of the special punctuation variables
will be going away or changing, but the default arg
is getting extended. In Perl5, $_ and @_ have
special magic, but %_ is only magical in the general
way; in Perl6, %_ will have special magic, as well.
(I believe L.W. is using the term 'topic' for this.)
Another difference in Perl6: in Perl5, all punctuation
variables are global. You can scope them dynamically
(with 'local'), but you cannot scope them lexically
(with 'my'). In Perl6, you will be able to scope them
lexically (err, whichever ones are kept, that is; as
I said, some of them are going away or getting
changed, renamed, or otherwise adjusted).
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
| [reply] [Watch: Dir/Any] [d/l] |
Re: Undocumented variable?
by dpmott (Scribe) on Sep 12, 2003 at 21:29 UTC
|
Sorry, forgot to mention that I first found this on Perl 5.8.0 from ActiveState, but it also happens with Perl 5.8.0 built for i86pc-solaris.
-dpmott
| [reply] [Watch: Dir/Any] |
|
|