in reply to Howto include Perl Script into CGI script

Sniff, sniff... the smell of ... homework? :-)

Ah well, I'll give you some hints.

You could make the second file into a module. (Read up on "package" and "use" it from your cgi file.) Implement it as a subroutine. You must put the previous shell-script (now module) in the Perl include path, as seen in the @INC array.

To give an error message, check the return code of your new subroutine and show another html page.

(You could also run the Perl script by using back-quotes, shell-script style.)

Update: Added detail. Since the asking monk is much more experienced than me, I feel nervous about answering. But if he want to troll me more, I'll be happy to add more details. :-)

  • Comment on Re: Howto include Perl Script into CGI script

Replies are listed 'Best First'.
Re^2: Howto include Perl Script into CGI script
by monkfan (Curate) on May 17, 2006 at 09:37 UTC
    (You could also run the Perl script by using back-quotes, shell-script style.)
    Hi BerntB,

    Thanks for your answer. Can you be a bit specific? Example?
    Here is what I have at the moment:
    sub print_results { my @top = param('toppings'); print b('Customer name: '), param('customer'), br, "You ordered ", param('no_unit'), ' unit of ', param('cone'), ' cone.'; print br; my $value = `perl compute_price.pl -type param('cone') -unit param +('no_unit')`; # Still doesn't show the value. What did I do wrong?? print 'Total price is', $value; }
    But it doesn't show the price as you try see from the link I have above. Is there anything I did wrong?
    It seem to me CGI script has different property with Perl script. Maybe backquote is not for it?

    Regards,
    Edward
      The "param('no_unit')" parts (inside the backquotes) are evaluated as shell script. Set them to variables and have the variables inside the "`".

      Update: Please note the dangers of "injecting" stuff like shell commands into your backquotes-eval if you use data that comes from the CGI! Use taint and/or the system() call with multiple parameters. (I.e. do system("perl", $param1, $param2);)

        Use taint and/or the system() call with multiple parameters.
        I learnt so much from your replies.
        How do you actually do the "tainting", in my OP above?

        Regards,
        Edward