jh has asked for the wisdom of the Perl Monks concerning the following question:

I have created a module with some regexes in it. In essence:
package MyRegex; our $rgx_number = qr/[0-9]/; our $rgx_letter = qr/[a-zA-Z]/; # etc.
When I use this module, I always want everything imported (don't worry, this is never going anywhere near CPAN). I know I can add everything to @EXPORT but I would prefer to write a custom export_to_level function that just always exports all variables.

I know how to export a symbol if I have the name, but I don't know how to get the names of all the symbols in the package.

I am sure there is a CPAN module that does this but that fly in the ointment is that I cannot install CPAN modules on this server. I can only use standard Perl 5.8 modules... :-(

Any ideas?

Thanks

Replies are listed 'Best First'.
Re: export all symbols
by merlyn (Sage) on Jan 21, 2009 at 17:16 UTC
      That works excellently, especially since all of the variables I want to export start with the prefix "$RGX":
      our @EXPORT = do { no strict 'refs'; map { '$' . $_ } grep { m/^RGX/ } keys %{ 'MyRegex::' }; };
      Thanks for the help!
      That's a good one... I'm glad I saw it before I posted my solution (which I've long since given up on because it uses a source filter).
Re: export all symbols
by marcussen (Pilgrim) on Jan 22, 2009 at 00:00 UTC

    Not trying to be insulting, but, you do know that perl supports several character classes that match what you are using. Such as \d = [0-9] as well as inverted patterns, \D = [^0-9], etc.

    Then again, assuming that you are familiar with perlre, you might be glad to know that you can add your own or CPAN's modules to where ever you can write files. It does not have to be a global install for it to work. I am assuming that you could spare a few Kb's worth of diskspace as you said server, not embedded system

    Confucius says kill mosquito unless cannon
      Those simple regexes were just examples... no one on here wants to see my actual 4-line regexes. :-)
Re: export all symbols
by Zarchne (Novice) on Jan 24, 2009 at 01:41 UTC
    For your problem to the extent you have described it, I suspect you really just want to leave off "package MyRegex" at the beginning and then "require" the module rather than "use"ing it, eh?
      That might have been easier if I had thought of it first, although it disrupts my nice structure of
      use Mycompany::CustomModule1; use Mycompany::CustomModule2; use Mycompany::CustomModule3;