Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Subroutine arguments problem

by dpatrick (Scribe)
on May 03, 2001 at 22:33 UTC ( #77733=perlquestion: print w/replies, xml ) Need Help??

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

This is returning "Not enough arguments for main::queryProcesses" when called from the if block. But I only specified one argument in the subroutine declaration. I don't understand what's going on.
In addition, use strict tells me that "global symbol $server requires explicit package name". I do not wish to have this be a global variable, so how would I declare it? Here is the relevant code:
#!/usr/local/bin/perl -w use strict; use CGI; use Proc::ProcessTable; my $statusCGI = new CGI; # Configuration variables our $acmePath = "/usr/local/acme"; our $universedBin = "universed"; our $worldBin = "world"; #our %universed = ("status", "server", "uptime", "user", "PID"); our %universed = (); #our %world = ("status", "server", "uptime", "user", "PID"); our %world = (); sub queryProcesses($server) { my $process = new Proc::ProcessTable(); my $processTable = $process->table; my $processTableRecord; foreach $processTableRecord (@{$process->table}) { if ($processTableRecord->fname eq $server) { %{$server} = ( pid => '$processTableRecord->pid', server => '$processTableRecord->fname' ); print $processTableRecord->uid, "\n"; } } } if (!$statusCGI->param("universedAction") && !$statusCGI->param("world +Action")) { queryProcesses("$universedBin"); queryProcesses("$worldBin"); }
Any insight as to what's going on would be appreciated.
dpatrick

Replies are listed 'Best First'.
(Ovid ) Re: Subroutine arguments problem
by Ovid (Cardinal) on May 03, 2001 at 22:46 UTC
    Perl thinks you're trying to use a prototype. Change the beginning of your subroutine declaration to the following:
    sub queryProcesses { my $server = shift;
    However, I also see the following in your queryProcesses() subroutine:
    if ($processTableRecord->fname eq $server) { %{$server} = ( pid => '$processTableRecord->pid', server => '$processTableRecord->fname' ); print $processTableRecord->uid, "\n"; }
    That suggests that you may need to rethink your code. You probably wanted $server to be declared global, or better yet, have the subroutine return a reference to the hash.

    Last issue:

    pid => '$processTableRecord->pid',
    Single quotes will cause the whatever is in the quotes to not be interpolated. From what I see, I suspect that you probably just want to drop the quotes. If you do, don't just change them to double quotes as double quotes do not allow for interpolations of subs or object methods. I'd probably write that bit as follows (note that I changed the parentheses to braces to create the hashref):
    $server = { pid => $processTableRecord->pid, server => $processTableRecord->fname };

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Thanks much, to Everyone!
      dpatrick

      and thanks for the update Ovid!
Re: Subroutine arguments problem
by Anonymous Monk on May 03, 2001 at 22:42 UTC
    Use this instead:
    sub queryProcesses ($) { my ($server) = @_;
    The thing after the subroutine name isn't a parameter list like you would see in other languages, it's a prototype. See perldoc perlsub.
Re: Subroutine arguments problem
by geektron (Curate) on May 03, 2001 at 22:43 UTC
    you don't need to do it this: sub queryProcesses($server)

    the ( $server ) argument is probably being interpreted as a prototype.

    and your commented out hashes countain an odd number of entries. that's not right. it looks like you just need an arra, not a hash.

    and use strict is complaining because you use the $server variable before you declare its scope.

Re: Subroutine arguments problem
by Hero Zzyzzx (Curate) on May 03, 2001 at 22:47 UTC
    Change sub queryProcesses($server) { to the following two lines and it should work:
    sub queryProcesses { my ($server)=@_;
    When you pass variables to a sub, they are in @_, and you're never getting them out with your code. I'm sure another monk can give you a better explanation, but that's how I understand it. -DJCP

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://77733]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2023-03-23 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?