Re: functionality of .pl from .pm



> Hi there,
>
> i have a strange requirement where in i have certain functionality to
> be achieved by calling a perlscript. say script.pl <parameters>
> but i also have to seperate these functionalities in a module script.pm
>
> how woudl you design a case where in script.pm provides certain
> functions, yet be able to work as a script
>
> i could call script.pl from within script.pm's function. but i am
> looking for any other approachs as well
>
> thanks!
> lohti

I do this kind of thing all the time. Suppose I have a high-level
function -- produce a summary of all the rows of the wonko database table,
based on a couple of parameters. I want to be able to use this one
subroutine in two basically different ways -- from a simple
command-line tool - so I can say something like

lawrence /export/home/lawrence > billing-summary client 15173

And other times, I want it to run under machine control, from a CGI or
a Perl/Tk application.

The easiest way is to have the script "billing-summary" just be a thin
wrapper around the subroutine call.

--------------------------- begin perl code --------------------------
#!/usr/bin/perl

use strict;
use warnings;
use Wonko;

if ( my @response = Wonko::summarize ( @ARGV ))
{
print join("\n", @response );
} else
{
print "No response from summarize.";
}

---------------------------- end perl code ---------------------------

in my CGI program I might do something like

--------------------------- begin perl code --------------------------

if ( my @response = Wonko::summarize ( client => $q->param('client')) ) {
print $q->Tr( [ map { $q->td($_) } @response ] ) ;
} else {
print $q->Tr($q->td('No response data'));
}

---------------------------- end perl code ---------------------------

Now, if having to have two files: the Wonko.pm module and a
billing-summary wrapper script is too ugly for you, you can actually
do the following:

Here is an example of Wonko.pm

--------------------------- begin perl code --------------------------
package Wonko;
use base 'Exporter';
use strict;
use warnings;

use Carp;

our @EXPORT_OK = qw / summarize script / ;
our %EXPORT_TAGS = ( all => \@EXPORT_OK );

sub summarize
{
my %param = @_;
unless ( exists $param{client}) {
carp 'missing client parameter' ;
return ();
}
# fake an answer
return ( 'Total invoiced: 372.56', 'Aged 30 days: 144.31', 'Aged 60 days: 72.55', 'Aged 90 days: 33.11' );
}

sub script
{
if ( my @response = summarize ( @ARGV ) ) {
print "@response\n";
} else {
print "No such response\n";
}
}

1;

---------------------------- end perl code ---------------------------

Now I can do something like

lawrence /home/export/lawrence > perl -I/tmp/libperl -MWonko=script -e script client 15173

I can even get more clever

lawrence /home/export/lawrence > alias wonko-summary 'perl -I/tmp/libperl -MWonko=script -e script'

Now I can do

lawrence /home/export/lawrence > wonko-summary client 15173
Total invoiced: 372.56 Aged 30 days: 144.31 Aged 60 days: 72.55 Aged 90 days: 33.11

Hope this helps...

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - lawrenabae@xxxxxxxxxxxxx s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.







.



Relevant Pages

  • Re: functionality of .pl from .pm
    ... >> i have a strange requirement where in i have certain functionality to ... > function -- produce a summary of all the rows of the wonko database table, ... > billing-summary wrapper script is too ugly for you, ... > carp 'missing client parameter'; ...
    (perl.beginners)
  • SUMMARY: mass process
    ... Some mentioned expect script, use component Tcl etc, which I wish I had ... > Thanks and I will summarize. ... > Do you Yahoo!? ... Shopping - with improved product search ...
    (SunManagers)
  • Re: parsing HTML content
    ... a script which will collect and summarize content from intranet web ... By dump, I mean to read it the same way you would read a file ...
    (perl.beginners)
  • Re: List of files fetched via include() or require()
    ... > I want to be able to, at the end of an executed file, summarize what files it ... > script that depending on the situation handles several different files and ... Never mind, I found the function. ...
    (comp.lang.php)
  • Re: List of files fetched via include() or require()
    ... Savut ... > I want to be able to, at the end of an executed file, summarize what files ... > script that depending on the situation handles several different files and ...
    (comp.lang.php)