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.


In reply to (Ovid) Re(2): Setting Globals in a Base Class by Ovid
in thread Setting Globals in a Base Class by Ovid

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.