tags:

views:

627

answers:

6

I'm looking through perlop and perlsub pages of the Perl manual. There are many references about "magic" and "magical" here (just search any of them for the "magic"). I wonder why is Perl so rich in them.

Some examples:

print ++($foo = 'zz')            # prints 'aaa'       
printf "%d: %s", $! = 1, $!      # prints '1: Operation not permitted'
use warnings; my $i; print $i++  # no warning for uninitialized value
while (my $line = <FH>) { ... }  # $line is actually tested for definedness
+13  A: 

I wonder why is Perl so rich in them.

To make things easy.

You'll find that most "magic" in Perl is to simplify the syntax for common tasks.

Anon.
Let me rephrase: Perl seeks to shorten the syntax in order to make golfer's life easier.
LiraNuna
Does anyone know of any cases of perl syntax being added specifically for golfing? :)
gnibbler
@LiraNuna: In a word, 'bullshit'. If anything, I would say that Perl's magic is aimed at making programmers happy (to paraphrase Ruby-creator and Perl fan Yukihiro Matsumoto).
Telemachus
@LiraNuna: No, Perl seeks to make easy things easy. This does not imply support for golfing. Note also that Anon. said the magic is there to *simplify* the syntax, not to *shorten* it.
Dave Sherohman
Gosh, can't you guys take s joke? :)
LiraNuna
@LiraNuna In general, yes: I love a good joke. On SO, when it comes to Perl, probably not. There are too many tedious Perl-bashers here. None of it's funny any more.
Telemachus
+27  A: 

When a Perl feature is described as "magic":

It means that that feature is implemented by NBA star Magic Johnson. Whenever Perl executes "magic", it is actually sending an RPC call to a remote receiver implanted in Magic himself. He computes the answer, and then sends a return message. The use of Mr. Johnson for all the hard parts of Perl provides a great abstraction layer and simplifies porting to new platforms. It's way easier than, say, the Apache Portable Runtime.

Source: perrin on Perl Monks

It's official! Perl is more magical.

Hits from the following Google searches:

 25  site:ruby-doc.org     magic
 36  site:docs.python.org  magic
497  site:perldoc.perl.org magic
FM
+1 "Magic" Johnson - my favorite explanation.
Nele Kosog
+1  A: 

Perl's design philosophy is that simple things must be simple. This sounds good,and to some extent it is. However, there's a tradeoff involved: Making every simple thing a one-liner results in tons of special case hacks to save a few lines of code. Different people have different preferences regarding making simple operations within a language simple versus making the language specification simple. Perl is at one extreme. Java is at the other, at least among languages that people actually use. Python and C# are somewhere in between.

dsimcha
Perl makes simple things simple ... until you need a dictionary of arrays, anyway.
Matthew
@Matthew: Huh? A hash (Perl's name for what Python calls a dictionary) of arrays is trivial. e.g. `my %h = (a => [1..3], b => [4..6]);` Indeed, once you're comfortable with the basics of references it's easy to create arbitrarily complex data structures.
Michael Carman
@Michael: Yes indeed ... once you're comfortable with references and their esoteric syntax: `@{$h{'a'}}`, `$h{'a'}->[2]`, etc.
Matthew
@Matthew Sometimes you want a copy of reference `$h{a}` , and sometimes you want a copy of the element(s) in a reference `@{ $h{a} }` . Also `$h{a}->[2]` is better written as `$h{a}[2]`
Brad Gilbert
@Brad - I must disagree here. Omitting "->" is IMHO Very Bad JuJu for non-golf purposes since it decreases readability. (I wonder if Conway said anything on the topic?)
DVK
DVK, I think you're in the minority. `$h{a}->[2]->[5]->{foo}` ... yuk! Anything other than the top level has to be a reference, and making it explicit makes it harder to read.
rjh
+3  A: 

I think (opinion more than fact) that this has to do with the organic growth viewpoint that Perl's creator Larry Wall has on the Perl language. An study in the opposite approach is to compare Perl with Python, whose style often makes Perl hackers cringe at the perception of being forced to conform to a stylistic regime.

Some of it has to do with Perl being designed to be "efficient" at writing quick scripts to do Perl*-ish* tasks, in both wall clock time, and in keystrokes. Some of it has to do with the TMTOWTDI mantra of Perl and its followers.

Programmers tend to be opinionated on Perl's frequent usage of "magic", for some it is an eye-straining visual cacophony of chaos and disrespect for orderliness (which harkens back to the days of computer Priesthood in white lab coats behind a glass window), for others it is a shining example of getting things done efficiently, if not always obviously to the novice or outsider.

mctylr
+9  A: 

Because perl always Does What I Mean for some values of always.

Sinan Ünür
And some values of "mean"!
Ether
ROFL... best use of "for some values" I've seen in a couple of years :)
DVK
+14  A: 

Magic, in Perl parlance is simply the word given to attributes applied to variables / functions that allow an extension of their functionality. Some of this functionality is available directly from Perl, and some requires the use of the C api.

A perfect example of magic is the tie interface which allows you to define your own implementation of a variable. Every operation that can be done to a variable (fetching or storing a value for instance) is exposed for reimplementation, allowing for elegant and logical syntactic constructs like a hash with values stored on disk, which are transparently loaded and saved behind the scenes.

Magic can also refer to the special ways that certain builtins can behave, such as how the first argument to map or grep can either be a block or a bare expression:

my @squares = map {$_**2} 1 .. 10;
my @roots   = map sqrt, 1 .. 10;

which is not a behavior available to user defined subroutines.

Many other features of Perl, such as operator overloading or variables that can return different values when used with numeric or string operators are implemented with magic. Context could be seen as magic as well.

In a nutshell, magic is any time that a Perl construct behaves differently than a naive interpretation would suggest, an exception to the rule. Magic is of course very powerful, and should not be wielded without great care. Magic Johnson is of course involved in the execution of all magic (see FM's answer), but that is beyond the scope of this explaination.

Eric Strom
ysth
Eric Strom
ah, sorry, you're right
ysth
Brad Gilbert
Eric Strom