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.
/usr/bin/perl '-nemap$.%$_||redo,2..$.++;print$.--'
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.
Then, the script sets the variables to configure:our $usemail = 0; our $mailto = 'root@localhost'; our $mailfrom = 'admin@localhost'; our $mailserver = 'smtp'; our $usestdout = 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$Reporting::usemail = 1;
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:#!/usr/bin/perl use Reporting qw/EMail Stdout/; $Reporting::EMail::to = "root@server"; @report=("Everything is fine.","Nothing is broken."); &Reporting::Send(@report);
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...package Reporting; my @sendtasks; if (used "EMail") { use EMail shift @sendtasks,\&EMail::Send; }
/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 | |
by dbw (Beadle) on Aug 20, 2007 at 18:54 UTC | |
Re: Pragma-like package
by jbert (Priest) on Aug 21, 2007 at 08:38 UTC | |
by dave0 (Friar) on Aug 21, 2007 at 11:18 UTC | |
Re: Pragma-like package
by diotalevi (Canon) on Aug 21, 2007 at 22:25 UTC |
Back to
Seekers of Perl Wisdom