开发者

Why is there so much "magic" in Perl?

开发者 https://www.devze.com 2022-12-20 17:28 出处:网络
Looking through the perlsub and perlop manpages I\'ve noticed that there are many references to \"magic\" and \"magical\" there (just search any of them for \"magic\"). I wonder why is Perl so rich in

Looking through the perlsub and perlop manpages I've noticed that there are many references to "magic" and "magical" there (just search any of them for "magic"). I wonder why is Perl so rich in them.

Some examples:

print ++($foo开发者_StackOverflow = 'zz')            # prints 'aaa'       
printf "%d: %s", $! = 1, $!      # prints '1: Operation not permitted'
while (my $line = <FH>) { ... }  # $line is tested for definedness, not truth
use warnings; print "0 but true" + 1  # "0 but true" is a valid number!


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


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.


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.


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


I think (opinion more than fact) that this has to do with the organic growth viewpoint that Perl's creator Larry Wall has with the Perl language. Python is a study in the opposite approach, 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 about 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消