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

In the following code, when I pass variables to get_status my system call is not working. If I hardcode the values in it works. I have used Data::Dumper to pass the variables out to the web page and they look fine, the system call just doesn't run - or perhaps isn't passing the data back to the web page.

#!/usr/bin/perl -T # prtadm.cgi # printer admin page use strict; use warnings; use diagnostics; use CGI qw/:standard/; use POSIX qw(strftime); use Data::Dumper; # For testing # Clean up our UNIX environment # for more infor read perldoc perlsec $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; ####### Declarations ####### my ($SERVER, @LINE, %svr_hash); my (@SERVERS, @TMP_); my $now=strftime "%m/%d/%Y %H:%M:%S", (localtime); my $configfile="/var/www/cgi-bin/prt.cfg"; ### End Declarations ####### ### Pull in configs ### open (CFGFILE,$configfile) || die "unable to open $configfile: $!\n"; foreach $_(<CFGFILE>) { next if /^#/; next if /^\s/; chomp; @TMP_=split(/,/,$_); push (@SERVERS,[ @TMP_ ]); } my @SERVER_LIST=map($_->[0], @SERVERS); for my $i (0 .. $#SERVER_LIST) { $svr_hash{$i} = "$SERVER_LIST[$i]"; } my %rev_srv_num=reverse %svr_hash; ## Start HTML ## print header, start_html; print "<tt><center>",br; print h3("AWI", "Printer", "Administration"); print "$now</center>\n"; print hr,br; ## End of header ## print start_form; print "\n<div align=right>Choose Server",br, popup_menu(-name=>'SERVER', -values=>[@SERVER_LIST], -default=>[$SERVER_LIST[0]]), submit('go'), "\n</div>",br; $SERVER=param('SERVER'); if (param('SERVER')) { print "Server $SERVER is number $rev_srv_num{$SERVER}\n",br; print "is default: @SERVERS[$rev_srv_num{$SERVER}]->[1]\n",br, "print subsystem: @SERVERS[$rev_srv_num{$SERVER}]->[2]\n",br, "connection type: @SERVERS[$rev_srv_num{$SERVER}]->[3]\n",br; get_status("@SERVERS[$rev_srv_num{$SERVER}]->[2]","$SERVER"); #get_status("cups","rlinux1"); } else { print "Error\n";br; } # End else print end_html; sub get_status { my $print_subsystem=shift; # chomp ($print_subsystem); # not needed my $server_name=shift; # chomp ($server_name); # not needed print "you passed $print_subsystem for server $server_name\n",br; print Dumper $print_subsystem; print Dumper $server_name; my @STATUS=`/usr/bin/lpstat -h $server_name -o`; if ( "$print_subsystem" == "cups" ) { for $_(@STATUS) { print "$_ \n",br; } } else { print "unknown subsytem type: $print_subsystem \n",br; } } # end sub get_status

Thanks in advance
Ted

**********UPDATE***********
config file is in the form of:
Server,default,subsytem,connection
ie
server1,no,cups,cups
server2,default,aix,rsh

2005-03-11 Janitored by Arunbear - added readmore tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: passing vars to a sub
by RazorbladeBidet (Friar) on Mar 11, 2005 at 15:04 UTC
    get_status("@SERVERS[$rev_srv_num{$SERVER}]->[2]","$SERVER");


    I highly doubt this is what you want.
    First there's not a need to enclose in double quotes
    Second, if you are actually passing an array as your first parameter, that's why your sub isn't working.

    my $print_subsystem=shift; # chomp ($print_subsystem); # not needed my $server_name=shift;


    This will just get the first value of the array as print_subsystem and the second value as server_name (provided there's more than one value).

    If you actually want to pass an array as a parameter, pass the reference.

    Maybe check perlsub for more info.

    One other thing, very quickly - the string comparison operator is "eq" not "=="

    Best of luck!

    Update:

    As my gusto for immediately diving into the code to help usually overtakes my common sense, in the effort of not leading you down the wrong path, I suggest you follow Joost's suggestions first (that is, post how the code isn't working).
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
      "the system call just doesn't run - or perhaps isn't passing the data back to the web page."

      The system call is not passing the printer status back to the web page. running the command from the command line returns the expected output of my printers status.
        Does
        print "you passed $print_subsystem for server $server_name\n",br;
        print out the same thing both times? (hardcoded and passed in)? can you try trammell's suggestion and see what the result is?
        --------------
        It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: passing vars to a sub
by trammell (Priest) on Mar 11, 2005 at 15:08 UTC
Re: passing vars to a sub
by simonm (Vicar) on Mar 11, 2005 at 15:59 UTC
    You might have more luck getting help here if you made a copy of this script and tried removing some of the "extra" pieces, so that we're left with the smallest piece of code that still has this problem.

    As an aside, in "$print_subsystem" == "cups", you need to replace the == (numeric equality comparison) with eq (string equality).

Re: passing vars to a sub
by Joost (Canon) on Mar 11, 2005 at 15:05 UTC