Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Putty Session Generator

by d_jabsd (Acolyte)
on Oct 25, 2004 at 16:26 UTC ( [id://402257]=note: print w/replies, xml ) Need Help??


in reply to Re: Putty Session Generator
in thread Putty Session Generator

Thanks for your input, thor! I'm still trying to get a handle on the intricacies of Perl, so all advice is greatly appreciated.
Just to clarify, I can replace  use strict 'vars'; with  use strict; ?
Could you explain item 2 a bit further, or point me to something that might help me fix it?
Thanks!

Replies are listed 'Best First'.
Re^3: Putty Session Generator
by thor (Priest) on Oct 25, 2004 at 18:36 UTC
    Just to clarify, I can replace use strict 'vars'; with use strict; ?
    For this script, you can. I find that coding under the full strictitures keeps me from doing things that I probably didn't mean, and is not that much harder than doing so without.
    Could you explain item 2 a bit further, or point me to something that might help me fix it?
    Sure. You're calling subs right now like mysub($foo, $bar) where the definition of mysub is something like
    sub mysub { $foo ++; some_other_sub($bar); }
    It should read like this:
    sub mysub { my ($foo, $bar) = @_; #do stuff with $foo and $bar }
    This allows you to follow another maxim that should be adhered to in general: declare your variables in the tightest scope that you can. That is to say that if you're using a while loop and some values only hold true for only that iteration of the while loop, you should declare them within the scope of that loop. For a concrete example from your code:
    my ($folder, $server_name, $session_name, $protocol, $hexport, $version, $compr, $user); while (<SERVER_LIST>) { next if /$ignore/ ; chop; ( $folder, $server_name, $session_name, $protocol)= split (":"); ($hexport,$version,$compr)=create_port($protocol); $user=create_user($folder); &create_session($server_name, $session_name, $protocol, $user); &create_link($folder, $server_name, $session_name); }
    becomes (with some other differences sprinkled in)
    while (<SERVER_LIST>) { next if /$ignore/ ; # chomp is safer than chop...read the perldoc for # both of those functions (perldoc -f chop for chop chomp; my ($folder, $server_name, $session_name, $protocol)= split (":"); my ($hexport,$version,$compr)=create_port($protocol); my $user=create_user($folder); # the &sub syntax is not advised in most situations for reasons # that are slightly advanced, sub() suffices create_session($server_name, $session_name, $protocol, $user); create_link($folder, $server_name, $session_name); }
    I didn't look too carefully at the code that is called by this block, but I'm pretty sure that the subs create_session and create_link would have to be altered in such a way as to not use global variables to store their information.

    I hope this helps. If you have more questions, feel free to ask. I'm more than happy to help!

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      sub mysub { $foo ++; some_other_sub($bar); }

      should read

      sub mysub { my ($foo, $bar) = @_; #do stuff with $foo and $bar }

      and not

      sub mysub { my ($foo, $bar) = @ARGV; #do stuff with $foo and $bar }
        You're absolutely right. Brainfart...fixed. Thanks.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-25 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found