It's MAGIC!

Simon on 2001-05-18T02:24:15

Today I spent a lot of time trying to convince Sarathy of the utility of a generalised magic for arrays and hashes. It looks like Graham Barr grokked what I was getting at, so it isn't just me.

The idea is simple. When you use a special scalar in Perl, like, say, $/, (but not $_, because that's not only special, it's extra special) Perl looks up the scalar in the symbol table and finds that it has magic attached to it. This magic, called "\0" magic, means that instead of getting the value from the SV structure, Perl calls a function magic_get and returns the value from there. (It doesn't have to be magic_get - there are lots of different types of magic, which fire off different functions. Anyway, "\0" magic fires off magic_get.) magic_get looks at the name of the scalar, and basically does a big case statement to perform code appropriate for that scalar.

The end result is that if I want to add a new special variable - let's say $} - then I can tell gv.c that it should have "\0" magic, and insert the appropriate code to deal with it in magic_get.

For arrays and hashes, it's not that easy. Unfortunately, every single special array and hash has its own magic - and not just its own, but also a different type of magic that should be copied to the elements of the array or hash. So if you want to add more special variables, you have to cook up a new kind of magic and more virtual tables and functions and it's just horrid.

Sarathy and I want to fix this, but in very different ways. I want to introduce a new once-and-for-all generic magic for aggregates and their elements scalars; Sarathy wants to collapse everything into the "P" magic which handles tied arrays and hashes, saying it's faster.

I don't know, we'll see.

What else did I get done today? Not much. Spent a while doing some perl.com stuff; wrote this week's P5P summary; (really, really late) played too much video games and slept too late, but that's a given. Oh, and postponed my trip to Japan until after the exams. It's a shame, but it's the sensible choice. :(