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$.--'