Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Can i call a handmade perl module within a php file?

by Nik
on May 26, 2008 at 12:23 UTC ( #688512=perlquestion: print w/replies, xml ) Need Help??
Nik has asked for the wisdom of the Perl Monks concerning the following question:

Hello i have made i perl module called which is supposed to add +1 to every page counter depending on the perl page that calls the module. Thai is the following code:
package MyCounter; use strict; use Exporter 'import'; our @EXPORT = qw/counter/; # functions to export by default sub counter { my ($pagename, $db, $host) = @_; ($pagename) = ($pagename =~ m{([^/\\]+?)(?:\.[^/\\.]+)?$}); #======= Insert or Update pagecounter appropriately, then display i +t ======== 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 pa +gename = ?', undef, $pagename) unless( $host eq 'webmaster' ); my ($counter) = $db->selectrow_array('SELECT pagecounter FROM count +ers WHERE pagename = ?', undef, $pagename); return $counter; } 1;
and it works fine within perl because i have every perl script of mine that needs counting to call my perl module. for example from my i use
use MyCounter; #i declare the usage of MyCounter .... .... $counter = counter( $0, $db, $host ); #i call my module
Now the problem is how IF i can do the same thing, that is call the handmade perl module within from my every php script that need counting.

So please enlight me if this thing can happen and how to call it. All i want to do after the calling of the perl module is to print the counter for the specific page, thats all!

Replies are listed 'Best First'.
Re: Can i call a handmade perl module within a php file?
by tachyon-II (Chaplain) on May 26, 2008 at 12:58 UTC

    Using Perl Code from PHP shows you how.

    But given the simplicity of this script why would you not just do it in php and save firing up a perl interpreter to do such a trivial task?

    Personally I think the most efficient way to handle this would be to have your script as a small mod_perl CGI so all you need to do in your pages is put a link to it. The old trick used to be to stick the link in an img tag and have the CGI return a 1 pixel transparent gif.

    A log parser that runs across your access log will count pages quite happily for you and requires no changes to your pages.

Re: Can i call a handmade perl module within a php file?
by Samy_rio (Vicar) on May 26, 2008 at 12:56 UTC
Re: Can i call a handmade perl module within a php file?
by mwah (Hermit) on May 26, 2008 at 13:13 UTC

    Simple solution:

    1) write a wrapper script (

    use MyCounter; # declare the usage of MyCounter my $file = shift; my $host = shift; my $db = <enter dbi opening here>; # where's the DB coming from? ... $counter = counter( $0, $db, $host ); #i call my module print $counter;

    2) call your wrapper script in each PHP file via shell_exec(...):

    <?php $file = $_SERVER['SCRIPT_FILENAME']; $host = $_SERVER['SERVER_ADDR']; $output = shell_exec("perl /local/www/nik/ $file $host"); echo "<pre>$output</pre>"; ?>

    3) DON'T DO THAT. In PHP, write a module as you did in Perl, Include() it and invoke it the same way like you did in Perl. Use mysql_pconnect(...) for persistent database connections (if necessary).



      After coming back to this problem an reading tachyon-II's reply, I wondered if this couldn't simply be generalized away for every web file or script (from whatever source), with the goal of inserting a small image with the number of hits for every single page link on a site. The single Perl-Script (eg. in /cgi-bin/) behind this 'image-link' would simply find the link in question (to be counted up) in the $ENV{HTTP_REFERER} variable, handle the database stuff in the background and respond by creating an image (containing a number) on the fly.

      This would imply:

      1) generate an appropriate link scheme, like this:

      [any html or script here] ... <img src="/cgi-bin/" /> </body> </html>

      where the appendix /image.png is a $ENV{PATH_INFO} to help Internet Explorer to "do the right thing" ;-)

      2) we need an appropriate database scheme, for MySQL this would (assumed the database is named 'counting') read:

      ... $dbh->do( q{ CREATE TABLE counters ( pagename VARCHAR(640) NOT NULL , pagecounter INT(11) DEFAULT 0 , PRIMARY KEY (pagename) ) Engine=InnoDB charset=latin1 COMMENT='pagecounter' } ); ...

      ... so I chose the link (pagename) to be the PK of the table. This later enables MySQL to do an INSERT IF NOT EXISTS equivalent thing (its actually: 'INSERT IGNORE INTO counters (pagename) VALUES (?)', which is simply ignored if the page entry (PK!) exists.

      3) The script, which is straightforward. First, the database is updated (my DBI is rusty, please correct my glitches), then the hit-number image is created (via Imager) and sent to the browser. Database access stuff and a font file path for number-output has to be modified. '/cgi-bin/':

      I did set this up on my (Linux) Box and it works nice. So I decided to post it, maybe its of use for someone else.



      I decided to follow step No3 although i dont know any php :)
      The closest solution i came up with is the following, please correct my mistakes
      This is the function that i want to make a php module out of it so every php script can easily invoke the php module: This is just the function i dont know how to make it a php module...
      <?php function counter($pagename,$db,$host) { preg_match("{([^/\\]+?)(?:\.[^/\\.]+)?$}", $pagename, $matches); l +ist($pagename) = $matches; //======= Insert or Update pagecounter appropriately, then display + it ======== @$db->do('INSERT INTO counters (pagename, pagecounter) VALUES (?, +0)', null, $pagename); if ($host != 'webmaster') $db->do('UPDATE counters SET pagecounter + = pagecounter + 1 WHERE pagename = ?', null, $pagename); list($counter) = $db->selectrow_array('SELECT pagecounter FROM cou +nters WHERE pagename = ?', null, $pagename); return $counter; } ?>
      ...and after the creation of the module i will have to invoke it as follows(iam sure i have errors)
      $file = $_SERVER['SCRIPT_FILENAME']; $host = $_SERVER['SERVER_ADDR']; $db = mysql_pconnect("", "a3310457_counter", "a33 +10457_akis", "*****"); #i changed host and details here $counter = counter( $file, $db, $host );
      This became php coding now but it started as a perl question and ended like this (sorry for this)
      If someone knows a bit of php then please helpe me correct soem errore i have so i get this thing finally to work.
Re: Can i call a handmade perl module within a php file?
by marcussen (Pilgrim) on May 27, 2008 at 07:45 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://688512]
Approved by Erez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2017-04-29 01:00 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.