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.


In reply to (Ovid) Re: Re (tilly) 3: 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.