http://www.perlmonks.org?node_id=633904

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

I've been roped into writing a couple of scripts at work, and I noticed they share a lot of code. I'd like to pull that code out into a package.

They're scripts that report various statistics to DBAs---I won't go into the details---and they're both configurable to output to email, standard output, logfiles, or any combination of the above.

So I wrote a package that simply declares a bunch of our variables.

our $usemail = 0; our $mailto = 'root@localhost'; our $mailfrom = 'admin@localhost'; our $mailserver = 'smtp'; our $usestdout = 1;
Then, the script sets the variables to configure:
$Reporting::usemail = 1;
Okay, this works, but I don't like it. What I'd really like to do is be able to pull in chunks of my code with something like
#!/usr/bin/perl use Reporting qw/EMail Stdout/; $Reporting::EMail::to = "root@server"; @report=("Everything is fine.","Nothing is broken."); &Reporting::Send(@report);
So that the sort of reporting would be clear at a glance from the use line, and not be dependent on some long variables further down. In summary, I want to write a package that works sort of like use strict; I can give the use line a list of "options" and it behaves differently depending on what I gave it... something like the following pseudocode:
package Reporting; my @sendtasks; if (used "EMail") { use EMail shift @sendtasks,\&EMail::Send; }
Is this possible? Is this easy? Is this really obvious how to do? Am I way off base? Most package-writing tutorials have been really unenlightening. Please help, magnanimous monks...

/usr/bin/perl '-nemap$.%$_||redo,2..$.++;print$.--'

Replies are listed 'Best First'.
Re: Pragma-like package
by akho (Hermit) on Aug 20, 2007 at 18:42 UTC
    use Package LIST;
    is equivalent to
    BEGIN { require Package; Package->import LIST; }
    so, you can use Reporting::import for that sort of thing:
    package Reporting; my @sendtasks; sub import { my $class = shift; for my $task (@_) { .. do the things you want to do .. } }
      Superb, thanks ++akho! I'm hopeless with objects and classes, so you monks were my only hope. :)

      /usr/bin/perl '-nemap$.%$_||redo,2..$.++;print$.--'
Re: Pragma-like package
by jbert (Priest) on Aug 21, 2007 at 08:38 UTC
    import() has been mentioned, but if what you want to do is to selectively pull in only some methods and variables from the package into your namespace, you might want to look at the Exporter module, which provides an import for you which does just that.

    This is the way people used to write reusable perl code, prior to the advent of the current OO style. It's not considered fashionable or good style these days, but it certainly does beat having repeated code in multiple scripts.

      import() has been mentioned, but if what you want to do is to selectively pull in only some methods and variables from the package into your namespace, you might want to look at the Exporter module, which provides an import for you which does just that.
      You might also want to take a look at Sub::Exporter instead. It's a newer, better way of manipulating the symbols exported by a module.
Re: Pragma-like package
by diotalevi (Canon) on Aug 21, 2007 at 22:25 UTC

    See also the pragma pragma. If you upgrade to 5.10 today you've already got the feature you asked for.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊