Recently it was pointed out to me by Aristotle that code references and use strict; were not inherently incompatible. Where I had been using &{$_[0]}($_), I could replace that with $main::{$_[0]}($_)! I thought the idea of strict code references was fairly elegant and adopted the idiom on the spot. Now for what Paul Harvey calls, 'the rest of the story'.
It seems that this technique collides with another idiom I use when developing code-- sub AUTOLOAD. At first I thought that the problem was use strict;, but a little testing showed that AUTOLOAD was working as designed, directly invoking a non-existent functions defaulted as they should, no problem. Just not the hash reference! So it seemed that I could either have $main:: and use strict or AUTOLOAD and &{$_[0]}($_) but that I would have to give up AUTOLOAD if I wanted the former.
“That sucks!” was my predictable reaction to that little piece of news. But next I remembered TIMTOWTDI! and set about replacing the functionality of AUTOLOAD or more realistically, the functionality that I needed. The result follows. Given an AUTOLOAD like:
the fix (with a example call) looks like:sub AUTOLOAD { my $tree = shift; our $AUTOLOAD; print OUT "@@@ $AUTOLOAD(\$tree) @@@\n"; recurse($tree); }
Certainly not as magical as the real AUTOLOAD but it gives me back the missing feature without much effort and seems a pretty good exchange.sub coderef { if (exists($main::{$_[0]})) { $main::{$_[0]}($_[1]); } else { print OUT "@@@ main::$_[0](\$tree) @@@\n"; recurse($_[1]); } } sub startrule { my $tree = shift; foreach (@$tree) { if ( ref eq 'ARRAY' ) { if ( ref( @$_[0] ) eq 'ARRAY' ) { startrule($_); } else { coderef(@$_[0],$_); } } } }
Note: for those who wonder why I didn't stick with my original no strict "refs";, all I can say is that I'm compulsive! I don't like warning messages after a compile and I don't like doing with out use strict;...
--hsm
"Never try to teach a pig to sing...it wastes your time and it annoys the pig."| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |