Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

subroutines and arguments

by natty_dread (Acolyte)
on Apr 04, 2005 at 13:23 UTC ( #444660=perlquestion: print w/replies, xml ) Need Help??

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

I know there's a similar question out there, but it didn't help me. I'm trying to read a server list and perform actions on each server one at a time. However, I cannot access the variable. Here's some code:
open (SERVERLST, "< svr_daily.csv"); foreach my $svr (<SERVERLST>) { chomp $svr; &ls_engine; } sub ls_engine { my $ls_eng = $_[0]; system ("wdmlseng -e $ls_eng"); }
My problem line of code is passing the global var $svr into the sub. I've tried my $ls_eng = shift (@_) AND my $ls_eng = $_; also.........I'm sure it's something small.

Replies are listed 'Best First'.
Re: subroutines and arguments
by phaylon (Curate) on Apr 04, 2005 at 13:28 UTC
    "I cannot access the variable" is a very very unfortunate description of what you want to do.

    $svr is declared with "my", so it's lexical. Here, you will only find it in your for-loop. Try:
    for my $svr (<SERVERLST>) { chomp $svr; ls_engine ($svr); }
    And read up in 'perldoc perlsub' about what & actually does.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: subroutines and arguments
by Fletch (Bishop) on Apr 04, 2005 at 14:10 UTC

    And not directly related to your problem, but ALWAYS CHECK THE RETURN VALUE FROM open!

    open( SERVERLST, "<", "svr_daily.csv" ) or die "Can't open server dail +y CSV: $!\n";

      And while you're at it, don't pass a single argument to system, either. Especially if you're passing in stuff from a file. (I'd recommend checking the lines from the file, even with the following changes):

      open (SERVERLST, '<', 'svr_daily.csv') or die "Can't read from server daily CSV: $!"; foreach my $svr (<SERVERLST>) { chomp $svr; ls_engine($svr); } sub ls_engine { my $ls_eng = shift; system qw(wdmlseng -e), $ls_eng; }

      Update: per Fletch, fixed the mistake w/ qw(). Had previously been:

      system qw(wdmlseng -e $ls_eng);
        perl -le 'print for qw( $erm $do $you $really $want $qw? )'

        Remember qw() is a compile time construct. But yes, if you're not sure about the pedigree of the contents of the file you don't want to pass a single scalar to system.

        Update: Not to mention that qw() is equivalent to split(' ', q/STRING/) to begin with and hence wouldn't do any interpolation.

Re: subroutines and arguments
by deibyz (Hermit) on Apr 04, 2005 at 13:28 UTC
    Have a look at perldoc perlsub.
    You're not passing any parameter to your subroutine, try something like:
    foreach my $svr (<SERVERLST>) { chomp $svr; ls_engine($svr); }
    Hope it helps,
    deibyz
Re: subroutines and arguments
by maa (Pilgrim) on Apr 04, 2005 at 13:29 UTC

    why not try: ls_engine($svr); instead?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2023-10-03 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?