Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to call counter.pl script from another script

by Nik (Initiate)
on Feb 22, 2008 at 15:29 UTC ( [id://669581]=perlquestion: print w/replies, xml ) Need Help??

Nik has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i use the following code in all my pages to create or update a counter for each run of each script.
Because the scripts are gettign bigger and bigger at the time i need to create a seperate file to put this code in and then call within my each script to do its counter update job.
#======== Insert or Update pagecounter appropriately, then display it +========== my ($pagename) = ($0 =~ m{([^/\\]+?)(?:\.[^/\\.]+)?$}); eval { # Just ignore errors if the record already exists $db->do('INSERT INTO counters (pagename, pagecounter) VALUES (?, 0) +', undef, $pagename); }; $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename) unless( $host eq 'Admin' ); my ($counter) = $db->selectrow_array('SELECT pagecounter FROM counters + WHERE pagename = ?', undef, $pagename);
So, i have placed the above code in a file called counter.pl and i call it within each perl script as
include ("counter.pl");
My problem is i dont know how to call 'counter.pl' giving it the name of the refferer script tha called it.

Can you help plz?

Replies are listed 'Best First'.
Re: How to call counter.pl script from another script
by pc88mxer (Vicar) on Feb 22, 2008 at 15:55 UTC
    You could implement include with perl's require. Just use:

    require("counter.pl");
    When you use require, the code in the file you specified will be executed immediately. Usually for what you want to do your code would be put into a subroutine, and you would use something like:

    require("counter.pl"); insert_counter(); # can also pass arguments to this subroutine

    where insert_counter() is the name of the subroutine you defined in counter.pl. You could then pass subroutine arguments to it like the name of referrer script, the database handle, etc.

    For more info on subroutines, check out perldoc perlsub. When you need better encapsulation, you'll want to read up on how to create modules.

    Update: forgot that perl doesn't have include, and that it instead uses require.

      I'will follow your advice and try to use my first perl module then!

      I understand for the link you gave that in order to use this 'counter.pm' module within my index.pl file i have to use 'use counter.pm' right?

      Here is the code i written inside counter.pm

      package counter; use strict; my ($db, $pagename, $host) = @_; #======== Insert or Update pagecounter appropriately, then display it +========== eval { # Just ignore errors if the record already exists $db->do('INSERT INTO counters (pagename, pagecounter) VALUES (?, 0) +', undef, $pagename); }; $db->do('UPDATE counters SET pagecounter = pagecounter + 1 WHERE pagen +ame = ?', undef, $pagename) unless( $host eq 'Admin' ); my ($counter) = $db->selectrow_array('SELECT pagecounter FROM counters + WHERE pagename = ?', undef, $pagename);
      What i dont know is how to pass the variables $db, $pagename, $host inside the counter.pm module because those 3 variables are a prerequisite for the module to perform its task.

      Also the counter.pm module will have to return the variables $counter back to index.pl script so i can print it in the following line of index.pl

      Please excuse me as this is the first time im trying to use modules.

      ps. Do i need to put the above code in a sub inside counter.pm or creating a sub inside counter.pm isnr neccesary?

        Perhaps you should take a look at Simple Module Tutorial regards creating a module, and calling its functions.

        Martin
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to call counter.pl script from another script
by toolic (Bishop) on Feb 22, 2008 at 15:55 UTC
    Have you considered placing this code in a sub and moving the sub into a Perl module? If you are unfamiliar with modules, Simple Module Tutorial is a good place to start.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://669581]
Approved by digger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-16 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found