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

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.

  • Comment on Re: use strict;,$main::, and AUTOLOAD: Why can't we all get along?
  • Download Code

Replies are listed 'Best First'.
Re: Re: use strict;,$main::, and AUTOLOAD: Why can't we all get along?
by hsmyers (Canon) on Jul 18, 2002 at 13:29 UTC

    In reverse order, sorry about the typo, typically I'm using $_ as a complex array of arrays and get a little sloppy in my notation (hmmm, note to self-- sloppy in this context usually means 'possibly bug-ridden', needs checking). As far as tadman's approach goes, I've no objection other than the additional typing, which come to think of it might actually be a little clearer in terms of what I'm doing-- i.e. self documenting. Sorry you were irked-- I thought a routine that deals with call refs might as well be called that! I like your economy of style though even if you are not happy with the proposed solution.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

      Oh, the being irked was not personal, nothing to be sorry for. I just thought the code was somewhat unclear and proposed what I think might be more readable.

      As far as subroutine naming goes, I prefer to call subroutines after what they do, rather than what they receive (verb vs subject). But that's a whole different can of worms.. :-)

      Makeshifts last the longest.