Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Passing values to a Perl script from PHP

by Yary (Pilgrim)
on Mar 30, 2015 at 15:54 UTC ( [id://1121859]=perlquestion: print w/replies, xml ) Need Help??

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

I am posting this question & my response, which came into the CPAN::Testers list. This seems like a better place and I am directing the original poster to this node. Thanks to the fellow monks who helped this fellow out on the mailing list before now.

On Sun, Mar 29, 2015 at 11:26 PM, Uday Shankar Kintali wrote:

> Looking if I can pass on the user selected values ( in php form) to the perl script.

Since you are calling the script using "system", there are a few general ways of getting the caller (PHP in this case) parameters to the callee (Perl):
  1. Environment variables. If you can set them in PHP before calling system, you ought to be able to retrieve them in Perl using $ENV{Env_var_name}, replacing "Env_var_name" with whatever environment variable you want to read. This seems the safest to me for simple use.
  2. Command-line arguments. If in PHP you run "system 'perl_script.pl arg0 arg1 arg2'", then Perl will have the arguments in @ARGV. $ARGV[0] eq 'arg0' && $ARGV[1] eq 'arg1', etc. Be careful if any of your arguments have spaces or shell-special characters like quotes, pipes- unless PHP has a form of "system" that takes care of those for you. Perl's "system" will do so, if you pass "system" a list or array instead of a string/scalar, but I don't know about PHP, and you are calling "system" from PHP.
  3. Pipe the data into Perl. A quick scan of PHP documentation says to do something like this:
    $handle = popen('PerlCode.pl','w'); fwrite($handle,"here is some input\n"); fwrite($handle,"more input\n"); # etc.... pclose($handle);
    and then in Perl you can read it like:
    while (<STDIN>) { # Do something with the line we just read }
  4. Some mutually agreed upon data store: a file, a database, shared-memory cache, etc. Need to be careful about handing the right data to the right process, if there's any chance of there being multi-process, and also careful about data lifetime- cleaning up old values, but not too soon. I won't go into the details here.

Replies are listed 'Best First'.
Re: Passing values to a Perl script from PHP
by NetWallah (Canon) on Mar 30, 2015 at 16:38 UTC
    These are excellent suggestions for one-time parameter passing. (++)

    If there is higher volume, frequency, or network dependence or latency, he may need to consider other technologies, such as SOAP, REST, or a message bus.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Passing values to a Perl script from PHP
by jellisii2 (Hermit) on Mar 31, 2015 at 11:56 UTC
    Why not use YAML or JSON and be done with it? Both languages have multiple options for stringifying data structures.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found