in reply to Re: Setting Globals in a Base Class
in thread Setting Globals in a Base Class

I like that. The only problem that I see is that the base class also has functions that it exports and, under certain circumstances, will AUTOLOAD functions that don't exist, but which the client requests be exported. To get around that, I think I would need to trap those functions that I need, set the globals, and remove those functions from the import list. What do you think about the following (also untested :) code?

package Foo::Bar; sub import { my %handlers = ( DEFAULT_XFR => \&set_xfr, NO_LOG => \&disable_logging ); my @args; for my $i ( 1 .. $#_ ) { push(@args, [ $_[$i], $i ]), next if $_[$i] eq 'DEFAULT_XFR'; push(@args, [ $_[$i], $i ]), next if $_[$i] eq 'NO_LOG'; } # two loops because we don't want to splice @_ while looping over +it foreach my $arg ( @args ) { if ( exists $handlers( $arg->[0] ) ) { $handlers( $arg->[0] )->( $arg->[1], @_ ); } } Foo::Bar->export_to_level( 1, @_ ); } sub set_xfr { my $index = shift; if ( defined $_[$index + 1] and $_[$index + 1] =~ /^([A-Z]\d{4,5} +)$/ ) { $Foo::DEFAULT_XFR = $1; splice @_, $index, 2; } else { $Foo::DEFAULT_XFR = 'H00293'; splice @_, $index, 1; } } sub disable_logging { # same concept; }

Cheers,
Ovid

Update: I'd also need to set a flag to see if the module was required instead of used. A require won't call the import() method, thus necessitating that the INIT() function still remain as a fall-back (and it would only fire if the import wasn't called).

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re (tilly) 3: Setting Globals in a Base Class
by tilly (Archbishop) on Nov 07, 2001 at 20:33 UTC
    I have found the export_to_level function in Exporter to be unmaintained and unreliable. Its error handling in particular leaves something to be desired due to poor maintainance and bad misunderstandings about the internals of Carp that it is messing around with.

    I would therefore recommend using:

    goto &Exporter::import;
    rather than using export_to_level. Yeah, it is a hack. But it is a hack that works reliably.

      Interestingly, that's the solution I came up with last night.

      sub import { # two loops because we don't want to splice @_ while looping over +it if ( grep { /:(?:bar|baz)/ } @_ ) { my $sub = caller(0) . '::set'; { no strict 'refs'; *{$sub} = \&set; } } # using goto to avoid updating caller goto &Foo::import; }

      I wound up providing a &set function for the globals, but needed to make it available as both a method and a function. When a person calls my constructor, they get the new method. If they want to use the methods as functions, I install the method in their namespace and then use goto to call the &import method of the base class.

      For those who don't understand that last bit, goto with a sub after it replaces the current subroutine called with the subroutine that you're calling. This has the effect of not updating the call stack, thereby allowing the base class to export to the client, instead of my class.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.