Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

What's magic so "magic"?

by Anonymous Monk
on Mar 15, 2003 at 16:02 UTC ( [id://243308]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

All wise monks, I've been coding Perl for a short while (<6 months), and kept hearing and seeing the term - "magic" about ....? However, I cannot find any good, clear documentation/explanation about this term and its process/implications. Can anyone share with me some pointers? Thanks alot! Mid-Ager

Replies are listed 'Best First'.
Re: What's magic so "magic"?
by Elian (Parson) on Mar 15, 2003 at 16:30 UTC
    Magic variables are any variable that has, well, magic attached to it. Magic is an internal perl facility that lets perl intercept reads and writes to a variable and call some code to satisfy the read or write request.

    Tied variables, for example, have magic. When you read or write to a tied variable perl calls the code in the package the variable has been tied to. Many perl special variables are magic--for example $! is magic. When you read from it, perl goes and fetches the contents of errno, which is an integer that notes the last error a system call threw.

    There's not much to know or learn about magic in general unless you want to go dive into the innermost guts of perl, in which case perlguts and perlapi are the place to go. Generally you'll use a specific type of magic, such as tying, rather than just magic in general.

      Believe it or not, but I was going to reply in a similar way :) So I limit my contribute to the following link: PerlGuts Illustrated (I'd like to know what Gisle Aas used to produce those beatiful illustrations).
        He drew them up on graph paper and then wrote the PostScript code by hand that reproduced them.
      I agree the type of explanation of "magic" you provided, but also I would like to think there is a type of "magic" in general in Perl.

      So in the Perl world, "magic" has two sides of its meangings, but they actually agree with each other. If you look into things like hash tie, behind it, Perl simply calls subs like STORE, FETCH etc. on your behalf. And those STORE, FETCH, etc. can be defaults from perl, can be what you supplied thru Tie::Hash.

        While this is true for most magic in Perl, not all "magic" is implemented via magic variables. The docs list at least the following areas as "magic", which according to your "strict" definition aren't:
        • Auto-increment (and range) operator on strings "has a little extra builtin magic to it"
        • one and two argument open() is called "magic open"
        • goto &NAME is said to be "highly magical"
        So, as usual with Perl, things are a little more fuzzy.

        Update: This was supposed to be a reply to Elians comment, not to pgs.

        While it's OK to think that there's pixie-dust driven featuers inside of perl, it's not a good idea to call them magic, since that really does overlap with a feature explicitly called magic. It'd be like thinking of a feature as filehandle or hash, say, which wasn't perl's actual filehandle or hash feature.

        Perl's magic feature's definitely less often directly used than many things, but it does exist, so it's best to restrict what you label as magic to what actually is magic. Less confusion in the long run there.

Re: What's magic so "magic"?
by pfaut (Priest) on Mar 15, 2003 at 16:12 UTC
Re: What's magic so "magic"?
by Limbic~Region (Chancellor) on Mar 15, 2003 at 16:06 UTC
    Anonymous Monk,
    Try this. You should find what you are looking for and a whole lot more (deep magic).

    Cheers - L~R

Re: What's magic so "magic"?
by hawtin (Prior) on Mar 16, 2003 at 09:43 UTC

    There is a specific technical meaning to the word "magic" when discussing Perl. It comes from a more general term (see for example here). Older programmers (like me) tend to refer to this type of stuff as automagic (but that usage is becomming less common).

    In fact quite a lot of the "special" words used when talking about Perl are documented in the Jargon File (a.k.a. The Hackers Dictionary). It is a good thing to dip into when you are bored.

Re: What's magic so "magic"?
by tachyon (Chancellor) on Mar 16, 2003 at 10:07 UTC
    [root@www root]# make magic make: *** Don't know how to make `magic'. Stop.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: What's magic so "magic"?
by pg (Canon) on Mar 15, 2003 at 16:36 UTC
    Perl is a high level language. For functions you use, there is always a level of abstraction, which is more abstract than most of other languages do. Sometime this abstraction is just too abstract, and seems to the Perl users as if it is a magic.
      While that's sort of true, in Perl's case there's a very specific facility that is explicitly called magic.
        That's not just my personal thinking, if you read the thread links provided in the first two replys and other articles on internet, you can see many people use this term in this way.

        As I always suggested, lots of time, especially for this kind of non-black-and-white questions/terms/thoughts, different ideas simply have different focuses, and they can support each other to make a complete picture.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://243308]
Approved by diotalevi
Front-paged by Marza
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found