Again it depends. AUTOLOAD is more for use with object oriented programming; and indeed again strict will not complain if you call a sub as main->$subname($_). Of course the first parameter passed is now the classname, "main" in this case, and your subs will have to accomodate for that, by shifting it off first thing or however you choose to handle it.

I am slightly irked by the setup as displayed; coderef() is a rather unhelpful name and the distribution of decision making is a bit clumsy IMHO. I'd do something like this:

sub default_handler { print OUT "@@@ main::$_[0](\$tree) @@@\n"; recurse($_[1]); } sub dispatch { ref $_[0] eq 'ARRAY' ? \&startrule : $main::{$_[0]} || \&default_handler; } sub startrule { my $tree = shift; ref eq 'ARRAY' and dispatch($_->[0])->($_) foreach @$tree; }

I'm not happy with that either though.. I think a clean solution would require a dispatch mechanism separate from Perl's that does not rely on sub names. No propositions however as right now my brain won't cooperate.

Also I'm wondering why there are too many locations for tadman's pragma solution to be feasible? After all, you have to add the coderef() call to the same places that you'd have to add no strict 'refs'; to, unless I'm missing something important.

Quick note with regard to your snippets: you are aware that @$_[0] is actually a single element array slice? Since you want a scalar, you should write that as $$_[0] or $_->[0].

Makeshifts last the longest.


In reply to Re: use strict;,$main::, and AUTOLOAD: Why can't we all get along? by Aristotle
in thread 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.