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

Package file details: ---------------------
package Vm; use warnings; use strict; sub latest { my $late = `ssh root\@10.10.21.15 'cat /usr/local/LATEST_VULN'` +; chomp($late); print "$late\n"; } 1; ====================================================== perl script using above package called "Vm" #!/usr/bin/perl use warnings; #use strict; use Vm; my $lat = Vm::latest(); print $lat; sub RedHat { my $path; $path = `ssh root\@10.10.21.15 'cat /usr/local/$lat/qscheme/redhat_com +p.qsv'`; chomp($path); open(FILE,'>redhat.txt'); print FILE $path; close FILE; open(REDHAT,'<redhat.txt'); while (<REDHAT>) { chomp; if (/$ARGV[0]/) { print "$ARGV[0] exists in Redhat_comp.qsv\n"; } } } RedHat($ARGV[0]);
===================================================

Question: When I run my perl script it says: VULN-1.24.80-1 1 cat: /usr/local/1/qscheme/redhat_comp.qsv: No such file or directory

It basically does not take "VULN-1.24.80-1" in path, but instead it takes 1, what changes should I make to script or package that it starts taking "VULN-1.24.80-1" in path ?

Thanks!!!

Replies are listed 'Best First'.
Re: Calling package function returns 1, not data from function
by toolic (Bishop) on Dec 23, 2009 at 03:31 UTC
    Change:
    print "$late\n";
    to:
    return $late;
    From the return doc:
    (Note that in the absence of an explicit return, a subroutine, eval, or do FILE will automatically return the value of the last expression evaluated.)
    And from the print doc:
    Returns true if successful.
    So, your 'latest' sub is returning the value of the last expression, which is the return value of print, which is 1 (it must have successfully printed).
      #!/usr/bin/perl use warnings; use strict; sub latest { my $late = `ssh root\@10.10.21.15 'cat /usr/local/LATEST_VULN'` +; chomp($late); } print "$late\n";

      The result of above script shows as:

      <p>/Perl$ ./latest.pl Global symbol "$late" requires explicit package name at ./latest.pl li +ne 11. Execution of ./latest.pl aborted due to compilation errors.</p>

      When I use undermentioned subroutine as updated by you, it displays error like this:

      ERROR:

      ~/Perl$ ./latest.pl Global symbol "$late" requires explicit package name at ./latest.pl line 11. Execution of ./latest.pl aborted due to compilation errors.

      #!/usr/bin/perl use warnings; use strict; sub latest { my $late = `ssh root\@10.10.21.15 'cat /usr/local/qualys/LATEST +_VULNSIGS'`; chomp($late); print "$late\n"; } return $late;
      Please assist. I am sure I am doing some stupid mistake. Thanks!!!
        You didn't follow the instructions. The sub should look like:
        sub latest { my $late = `ssh root\@10.10.21.15 'cat /usr/local/LATEST_VULN'`; chomp($late); return $late; }
        Nothing else should be changed. Well, ok, one thing.
        print $lat;
        should be changed to
        print "$lat\n";