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:

sub AUTOLOAD { my $tree = shift; our $AUTOLOAD; print OUT "@@@ $AUTOLOAD(\$tree) @@@\n"; recurse($tree); }
the fix (with a example call) looks like:
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],$_); } } } }
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.

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."

In reply to use strict;,$main::, and AUTOLOAD: Why can't we all get along? by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.