>>>>> "MR" == Matteo Riva <mura...@gmail.com> writes:
MR> After reading Uri's post about projects for beginners, I took a quick MR> look at File::Slurp source code, and I was stuck at the very beginning. looking at my code is a good way to learn perl! :) MR> # Install subs for various constants that aren't set in older perls MR> # (< 5.005). Fcntl on old perls uses Exporter to define subs without a MR> # () prototype These can't be overridden with the constant pragma or MR> # we get a prototype mismatch. Hence this less than aesthetically MR> # appealing BEGIN block: MR> BEGIN { MR> unless( eval { defined SEEK_SET() } ) { that line is actually incorrect and has been fixed in an unreleased version (to be released soonish). MR> *SEEK_SET = sub { 0 }; MR> *SEEK_CUR = sub { 1 }; MR> *SEEK_END = sub { 2 }; MR> } MR> and more to follow wich - to my understanding - define constants which MR> act as flags with usual binary ANDs and ORs. Since the quoted comment MR> didn't really shed much light on my ignorance, I'm here to ask exactly MR> what that code does. Probably the main point I'm missing is the use of MR> typeglobs? Any pointer to a better explanation of the topic - if it's MR> too wide for a single email reply - would be very much welcome. typeglobs are effectively ways to access the symbol table in perl. to make the story short, each global name (e.g. SEEK_SET) has a glob structure with entries for each possible perl variable type (e.g. scalar, array, hash, code, etc.). each entry hold a reference to the actual variable if it was assigned or declared. this is normally done by the compiler but perl can modify typeglob entries at runtime which is what i am doing there. if the entry wasn't define (as i said that test is broken there) from loading a module (older perls didn't define those constants), then i assign code refs to the glob code slot so it will be seen by the later code that uses it. this is also why this is done in a BEGIN block, so it happens at compile time. constant subs must be declared at compile time so they get optimized into real constants by the compiler. hopefully that explains it. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/