Input parameter for perl CGI script

682 views Asked by At

I need some insight on my Perl CGI script.

First of all all this is running under webmin so i'm doing a custom module.

I'm calling a CGI Perl script passing 2 parameter from another Perl CGI. The link I'm calling is in the following format:

http://IP:8080/foobar/alat.cgi?sysinfo=xxxxxxx&SR=yyyyyyyy

The alat.cgi script look like this

#!/usr/bin/perl
use CGI qw(:standard); 
ReadParse();
$q = new CGI;
my $dir = $in->param('SR');
my $s = $in->param('sysinfo');
ui_print_header(undef, $text{'edit_title'}.$dir, "");
print $dir."<br>";
print $s"<br>"; 

The only output I get printed is the value of $dir and $s seems to be empty.

What am I doing wrong?

3

There are 3 answers

1
simbabque On BEST ANSWER

As @Сухой27 said, add use strict;, but also use warnings; to the top of your script, right below the shebang (#!/usr/bin/perl) line. Those will tell you about syntax errors and other stuff where Perl is doing something other than you might intend.

With CGI (which is btw not part of the Perl core in the latest 5.22 release any more) and the object oriented approach you are tyring to take, you don't need to use ReadParse(). That is an abomination left in from Perl 4's cgilib.pl times.

I don't know what your ui_print_header function does. I'm guessing it outputs a bunch of HTML. Are you sure you defined it?

With fixing all your syntax errors and using modern syntax, your program would look like this. I'll break down what is happening for you.

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

my $q = CGI->new;
my $dir = $q->param('SR');
my $s = $q->param('sysinfo');

# you need to declare this to use it below
my %text = ( edit_title => 'foo' );

# we declare this sub further down
ui_print_header(undef, $text{'edit_title'} . $dir, q{});

print $dir . '<br />';
print $s . '<br  />';

sub ui_print_header {
  my ( $foo, $title, $dir, $bar ) = @_;

  # do stuff here...
}

Let's look at some of the things I did here.

  • Saying new CGI as the CGI docs suggest is fine, but since we are using the OOP way you can use the more common CGI->new. It's the same thing really, but it's consistent with the rest of the OOP Perl world and it's more clear that you are calling the new method on the CGI package.
  • If you have $q, keep using it. There is no $in.
  • Declare all your variables with my.
  • Declare %text so you can use $text{'edit_title'} later. Probably you imported that, or ommitted it from the code you showed us.
  • Declare ui_print_header(). See above.
  • q{} is the same as '', but it's clearer that it's an empty string.
1
Cris On

thank you everyone for the very quick answer, and as I was suspecting I just had some silly mistake. Adding here the corrected code that now works

#!/usr/bin/perl
# Run alat on selected sysinfo and allow display of output
#use strict; 
use diagnostics;
require 'recoverpoint-lib.pl';
use CGI qw(:standard); 
ReadParse();
my $q = new CGI;
my $dir = $q->param('SR');
my $s = $q->param('sysinfo');

ui_print_header(undef, $text{'edit_title'}.$dir, "");
print  $dir."<br>";
print  $s."<br>";

Just to clarify for some of previous answer, this is a custom module of webmin so variable $text is imported and function ui_print_header is a webmin defined one, it basically print the page header in HTML

0
Arunesh Singh On

As you enable strict and warnings you can easily know the errors.Also you should check Apache error logs, I think the script should be like this:

#!/usr/bin/perl
use CGI qw(:standard); 
use strict;
use warnings;
ReadParse();
my $q = new CGI;
my $dir = $q->param('SR');
my $s = $q->param('sysinfo');
ui_print_header(undef, $text{'edit_title'}.$dir, "");
print $dir."<br>";
print $s."<br>";