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

I am trying to get my perl script to work or be called from the web. That way you don't have to access my linux box to run it. You can just goto a webpage and run it from there. To run my script you do this:
perl push2.pl -i (ip address) -s (tftp server) -c (config file) <enter +>
I have posted my script on my scratchpad and made it public. There is one more thing bout my script. It works fine using the command above via command line. But if i just type:
perl push2.pl
I get this error:
Use of uninitialized value in concatenation (.) or string at push2.pl +line 45. IP ADDRESS: Use of uninitialized value in concatenation (.) or string at push2.pl +line 74. Error: can't resolve "" to IP address at /usr/lib/perl5/site_perl/5.6.1/SNMP_util.pm line 400 Can't bless non-reference value at /usr/lib/perl5/site_perl/5.6.1/SNMP +_Session.pm line 860.
I would like to fix that also.
Thanks in advance

20030805 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: help with getting my perl script to run via web
by LameNerd (Hermit) on Aug 05, 2003 at 20:17 UTC
    It appears that your script needs some command line arguments. Maybe your script should contain some code that handle command line arguments. Something like this ...
    ... use strict; use Getopt::Std; use vars qw/ $opt_i $opt_s $opt_c $opt_h /; sub usage () { my $msg = shift || "-h prints this message"; print "$msg\nusage:$0 -i<ip addr> -s<tftp server> -c<config file> +\n"; } getopts( 'i:s:c:h' ); if ( !$opt_i ) { usage( "Please specify ip address" ); exit 1; } if ( !$opt_s ) { usage( "Please specify tftp server" ); exit 1; } if ( !$opt_c ) { usage( "Please specify config file" ); exit 1; } if ( $opt_h ) { usage; exit 0; } ...
      I use this idiom in my scripts:
      exit usage("Please specify ip address") unless $opt_i; exit usage("Please specify tftp server") unless $opt_s;
      ... and so on. usage is written so it returns 1 (typically).
Re: help with getting my perl script to run via web
by PhilHibbs (Hermit) on Aug 05, 2003 at 20:09 UTC
    Try adding:
    die "No IP address passed\n" if !defined $i;
    after $i = $opt{i}; Or, default it with something like:
    $i = "127.0.0.1" if !defined $i;
    There may be a better way of defaulting, but I've never used Getopt.
Re: help with getting my perl script to run via web
by liljunior (Initiate) on Aug 05, 2003 at 19:57 UTC
    Sorry bout the first post i am new to this forum. Here it is again and easier to read. :) I am trying to get my perl script to work or be called from the web. That way you don't have to access my linux box to run it. You can just goto a webpage and run it from there. To run my script you do this:
    perl push2.pl -i (ip address) -s (tftp server) -c (config file) <enter +>
    I have posted my script on my scratchpad and made it public. There is one more thing bout my script. It works fine using the command above via command line. But if i just type:
    perl push2.pl
    I get this error:
    Use of uninitialized value in concatenation (.) or string at push2.pl +line 45. IP ADDRESS: Use of uninitialized value in concatenation (.) or string at push2.pl +line 74. Error: can't resolve "" to IP address at /usr/lib/perl5/site_perl/5.6.1/SNMP_util.pm line 400 Can't bless non-reference value at /usr/lib/perl5/site_perl/5.6.1/SNMP +_Session.pm line 860.
    I would like to fix that also. Thanks in advance
Re: help with getting my perl script to run via web
by Massyn (Hermit) on Aug 05, 2003 at 22:19 UTC

    Mate,

    Your problem is that you're trying to run a command line written script as a CGI. That's why you get all the errors. Command line parameters work differently in command line driven apps, than with CGIs (You do know what a CGI is, right? In a nutshell, running your perl script on a webserver, what you're trying to achieve.

    The easiest way, would be use alter your code slightly...

    #!/usr/bin/perl -w use strict; use CGI; my $cgi = new CGI; my $i = $cgi->param("ip"); my $s = $cgi->param("server"); my $c = $cgi->param("config"); print "Content-type: text/html\n\n";

    Now you can call you app by running (as an example!) http://www.example.com/cgi-bin/myapp.pl?i=192.168.1.1&s=server.example.com&c=config.sys

    This might work, but you'll probably find that the formatting is all busted up. THe easiest way is to have

    print "<html><pre>\n";
    at the top and
    print "</pre></html\n";
    right at the bottom. At least you'll have a functional program. Now you have make it look nice with all the HTML stuff in between.

    Hope this helps.

    #!/massyn.pl

    Don't -- me without telling me why. If you asked a stupid question, I wouldn't down vote you.

      Being new to using cgi. How would i implement what you are saying into my script?