how can i convert my perl script to be a perl module?

G

georg.heiss

Hello,
how can i convert my perl script to be a perl module? Have i just
rename it to log2db.pm ?

#log2db.pl
use DBI;
use File::Tail;

my $fname = $ARGV[0];
#$fname = '/var/log/syslog';
my $host = $ENV{'HOSTNAME'};
my $dbh = DBI->connect("DBI:mysql:database=testdb;host=rettung",
"log4", "l12345", {'RaiseError' => 1});
my $table = 'log4perl';
my @fields = qw( log_timestamp level1 method message );
my $fields = join(', ', @fields);
my $sql = "INSERT into $table ($fields) values (?,?,?,?)";
my $sth = $dbh->prepare($sql);

$file=File::Tail->new(name=>$fname, maxinterval=>2, adjustafter=>5);
while (defined($line=$file->read)) {
$sth->execute(get_ts(),$host,$fname,$line);
$sth->finish();
}
$dbh->disconnect();

sub get_ts() {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$year = $year + 1900;
$mon = sprintf ("%02s",$mon+1);
$mday = sprintf ("%02s",$mday);
$hour = sprintf ("%02s",$hour);
$min = sprintf ("%02s",$min);
$sec = sprintf ("%02s",$sec);
my $ts = "$year$mon$mday $hour:$min:$sec";
return $ts;
}

Kind Regards
Georg
 
T

Ted Zlatanov

JE> One way: you could follow the suggestions in the FAQ.

JE> perldoc -q module

JE> "How do I create a module?"

That doesn't cover the specific case of an existing Perl script. Is
there something like that in the documentation? I don't recall it.

In any case, my advice is: avoid globals, pass all your state variables,
and you should be able to make all your script's subroutines exportable
in a namespace. If you're looking for OOP (and sometimes you must), you
have to examine your script's particular use cases and how you can
abstract state into an object hierarchy.

Ted
 
J

Jürgen Exner

Ted Zlatanov said:
JE> One way: you could follow the suggestions in the FAQ.

JE> perldoc -q module

JE> "How do I create a module?"

That doesn't cover the specific case of an existing Perl script.

You are right, it doesn't.
But it does provide a compact outline (the template) of how to organize
a module and what elements you would typically include. Based on that he
can make the call of which elements are needed in his case. The other
documentation (perldoc perlmod) is rather long and tedious to read.
Is
there something like that in the documentation? I don't recall it.

The real question is: what does the OP mean by "converting a program
into a module"?

The only thing a program exports is the 'main' function (well, strictly
speaking there is no such thing but you know what I mean). For the
module he will have to evaluate individually which elements he needs to
export for his application, if he needs initialization and cleanup code,
etc, etc.

jue
 
T

Ted Zlatanov

JE> You are right, it doesn't.
JE> But it does provide a compact outline (the template) of how to organize
JE> a module and what elements you would typically include. Based on that he
JE> can make the call of which elements are needed in his case. The other
JE> documentation (perldoc perlmod) is rather long and tedious to read.

JE> The real question is: what does the OP mean by "converting a program
JE> into a module"?

JE> The only thing a program exports is the 'main' function (well, strictly
JE> speaking there is no such thing but you know what I mean). For the
JE> module he will have to evaluate individually which elements he needs to
JE> export for his application, if he needs initialization and cleanup code,
JE> etc, etc.

In my experience, the question usually comes down to "I have functions
that should be available to multiple programs, and I realize that do()
is a nasty hack that will bite me sooner or later. So how do I put
together a library of functions as a module?"

This may be a wrong guess, but it's IMO very common... Pointing to the
Exporter module (`perldoc Exporter') is probably a good first step, in
addition to the module FAQ you pointed out.

Thanks
Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top