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

Re^2: How to call counter.pl script from another script

by Nik
on Feb 22, 2008 at 16:39 UTC ( #669598=note: print w/ replies, xml ) Need Help??


in reply to Re: How to call counter.pl script from another script
in thread How to call counter.pl script from another script

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?


Comment on Re^2: How to call counter.pl script from another script
Download Code
Re^3: How to call counter.pl script from another script
by marto (Chancellor) on Feb 22, 2008 at 16:43 UTC
    Perhaps you should take a look at Simple Module Tutorial regards creating a module, and calling its functions.

    Martin
      okey i will do that but please answering me this:

      a) A sub is needed when a particular piece of code needs to be run by the same script lots of times with different arguments?

      b) A module is needed when a particular piece of code needs to be run by the same or more than one script lots of times with different arguments?

      This is the question one has to answer prior of deciding if he needs a subroutine or a perl module?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://669598]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-09-17 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (85 votes), past polls