Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how can i convert my perl script to be a perl module?

Reply
Thread Tools

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

 
 
georg.heiss@gmx.de
Guest
Posts: n/a
 
      02-08-2009
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-08-2009
"" <> wrote:
>how can i convert my perl script to be a perl module?


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

perldoc -q module

"How do I create a module?"

jue
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      02-09-2009
On Sun, 08 Feb 2009 09:27:48 -0800 Jürgen Exner <> wrote:

JE> "" <> wrote:
>> how can i convert my perl script to be a perl module?


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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-10-2009
Ted Zlatanov <> wrote:
>On Sun, 08 Feb 2009 09:27:48 -0800 Jürgen Exner <> wrote:
>
>JE> "" <> wrote:
>>> how can i convert my perl script to be a perl module?

>
>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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      02-10-2009
On Tue, 10 Feb 2009 08:48:26 -0800 Jürgen Exner <> wrote:

JE> Ted Zlatanov <> wrote:
>> On Sun, 08 Feb 2009 09:27:48 -0800 Jürgen Exner <> wrote:
>>

JE> "" <> wrote:
>>>> how can i convert my perl script to be a perl module?

>>

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.


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.

>> Is
>> there something like that in the documentation? I don't recall it.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 8.30 How can I convert my shell script to perl? PerlFAQ Server Perl Misc 0 04-22-2011 04:00 AM
problem calling perl script from SOAP server perl script pj Perl Misc 3 04-09-2004 10:23 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57