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

Discussion in 'Perl Misc' started by georg.heiss@gmx.de, Feb 8, 2009.

  1. Guest

    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
    , Feb 8, 2009
    #1
    1. Advertising

  2. "" <> 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
    Jürgen Exner, Feb 8, 2009
    #2
    1. Advertising

  3. Ted Zlatanov Guest

    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
    Ted Zlatanov, Feb 9, 2009
    #3
  4. 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
    Jürgen Exner, Feb 10, 2009
    #4
  5. Ted Zlatanov Guest

    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
    Ted Zlatanov, Feb 10, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,757
  2. Maric Michaud
    Replies:
    0
    Views:
    7,173
    Maric Michaud
    Jun 24, 2006
  3. Justin Wyllie
    Replies:
    5
    Views:
    229
    Peter Pan
    Jan 11, 2005
  4. sm
    Replies:
    3
    Views:
    93
    Dave Weaver
    Jan 4, 2007
  5. PerlFAQ Server
    Replies:
    0
    Views:
    329
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page