Most revered monks,

I have the following cgi script, it can also be viewed in browsable form here.
What it does is simply compute the total price of the order.
#!/usr/bin/perl -w use CGI ':standard'; print header, start_html('Order Ice Cream with Price'), h1('Order Ice Cream with Price'); generate_form(); print_results() if param(); print end_html(); 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; # how to include the perl script "compute_price.pl" result here? print ('Total price is ...'); } sub generate_form { print hr, start_form, strong('Your name : '), textfield( -name => 'customer' ), br,br strong('Cone: '), radio_group( -name => 'cone', -multiple => 1, -values => [qw/sugar waffle/]),br,br strong('Number of Units: '), textfield( -name => 'no_unit'), br,br submit( -value => 'Send Order' ), end_form, hr; }
Now I have a perl script that does the computation. Typically it is executed in command line like this.
$ perl compute_price.pl -type sugar -unit 3
The perl script can be found below:
#!/usr/bin/perl -w use strict; use Data::Dumper; use Carp; use Getopt::Long; my $cone_type = 'sugar'; my $no_unit = 1; my $help = 0; if ( @ARGV == 0 ) { &usage(); exit(1); } my $res = GetOptions ( "type=s"=>\$cone_type, "unit=i"=>\$no_unit, "help"=>\$help,); if ($res) { die "Unit too large\n" if ($no_unit > 5); my %unitprice = ( 'sugar' => 100, 'waffle' => 200 ); my $total_price = $unitprice{$cone_type} * $no_unit; print "$total_price\n"; } sub usage { print <<HELP; Usage: perl $0 [-options] -h Print this help -type Cone type -unit No of Unit HELP exit; }
My questions are:
  1. How can I include "compute_price.pl" into the main cgi script above, so that it prints the total price?
  2. How can I also show the "die" version into the cgi-script when parameter given is incorrect. Just like in "compute_price.pl" ?

Regards,
Edward

In reply to Howto include Perl Script into CGI script by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.