Calling Perl script from PHP and passing in variables, while also using variablized perl script name

前端 未结 3 829
迷失自我
迷失自我 2020-12-02 02:17

I normally call perl scripts from PHP as below and pass in variables this way, and it works fine, however now I am building a component for re-use where I want to also varia

相关标签:
3条回答
  • 2020-12-02 02:22

    Your way is not working because you are concatenating all the parameters without spaces, effectively making them one parameter.

    Try

    $perlscript_file = "/var/www/other_scripts/perl/apps/$perlscript.pl $var1 $var2 $var3 $var4";
    

    By the way, if the parameters are coming from an external source, you MUST sanitize them using escapeshellarg(). The same goes for $perlscript - if it comes from an external source or even user input, do a escapeshellcmd() on it.

    0 讨论(0)
  • 2020-12-02 02:35

    In your second code, you're concatenating the variables without spaces between them. You should consider using sprintf to format this nicely:

    $script = sprintf('/var/www/other_scripts/perl/apps/%s.pl %s %s %s %s', $perlscript, $var1, $var2, $var3, $var4);
    
    0 讨论(0)
  • 2020-12-02 02:38

    On a sidenote, there is a CPAN package that aims to provide a bridge between PHP and Perl, which would allow you to do something like the following in PHP:

    $perl = Perl::getInstance();
    $instance = $perl->new('perlclass', @args);
    

    Not sure how stable this is though. See

    • PHP::Interpreter
    • Integrating PHP and Perl

    If you are using Apache, you can also use

    // PHP
    apache_note('foo', 'bar');
    virtual("/perl/some_script.pl");
    $result = apache_note("resultdata");
    
    # Perl
    my $r = Apache->request()->main();
    my $foo = $r->notes('foo');
    $r->notes('resultdata', somethingWithFoo($foo));
    

    See http://php.net/manual/en/function.apache-note.php

    0 讨论(0)
提交回复
热议问题