in reply to What's magic so "magic"?

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.

Replies are listed 'Best First'.
Re: Re: What's magic so "magic"?
by larsen (Parson) on Mar 15, 2003 at 16:38 UTC
    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.
Re: Re: What's magic so "magic"?
by pg (Canon) on Mar 15, 2003 at 17:06 UTC
    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.

        You mean like the way people say "I use CGI"?

        Makeshifts last the longest.