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

There's some intrinsic humor in the way you're uneasy about turning off reference checking, and yet you're perfectly willing to use AUTOLOAD.

Instead of turning off references completely, you can just turn them off locally. Like this:
sub foo { my ($foo_target) = @_; no strict 'refs'; # Up to no good here ${"${foo_target}::foo"}++; }
So the disengaged ref checking should be localised to that block.

No warnings. It's a lot easier than wrapping your calls in a "loader".
  • 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 17, 2002 at 22:43 UTC
    True! If it were not for the fact that the $main:: calls are scattered through out about 1000 lines of code I'd buy into your version. Frankly I haven't made up my mind yet about this being paranoid nonsense on my part or a semi-good idea...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      I think if you've got calls "scattered through" your code, it's time to think about what you're doing. I like to keep my Black Ops-style programming to very small areas, and wherever possible, use intermediate functions to get the goods. In, done, out, and nobody is the wiser.

      One way to avoid using AUTOLOAD is to auto-generate your functions. I've found this to be far more effective and flexible.

      You can do this like so:
      sub maker { no strict 'refs'; foreach my $func (qw[ foo bar baz ]) { *{"Foo::$func"} = sub { $func }; } } maker(); print join(',', Foo::foo(), Foo::bar(), Foo::baz()),"\n";
      Of course, your code is probably much more complex. Still, no warnings, and you can even use closures.

        The more I look at your first suggestion, the more I like it. I just counted, and there are only 13 locations where I in effect call a code reference. It would certainly be clearer to use the pragma and would act as a kind of documentation as well. As for the 'maker' idea, that isn't actually what I'm using AUTOLOAD for. It only acts as a stand in for code I haven't written yet. It allows me to 'run' without having to worry about the missing parts. The code in question is a compiler and the use of AUTOLOAD will go away in the production version.

        --hsm

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