Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Putty Session Generator

by thor (Priest)
on Oct 24, 2004 at 13:19 UTC ( [id://402027]=note: print w/replies, xml ) Need Help??


in reply to Putty Session Generator

I think I might use this once I disect it and (possibly) clean it up. Please take the following as instructive and not as derisive.

A couple of things that jumped out at me:

  • use strict 'vars'; Usually one needs a pretty good reason to enable only some of the strictitures. As it turns out, your code runs under full strict mode.
  • You're not passing variables to subs. You might think that you are with calls like $user=create_user($folder);, but the create_user sub (and all the others) use $folder (and the rest of the variables) in a global sense. This is usually considered bad form.
I might try using your script as a base for creating one that does the same thing, but using an XML file as a config so that you only have to specify the values that are not the default value. Plus, it would give me an excuse to learn perlXML...;). This is a great idea!

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

Replies are listed 'Best First'.
Re^2: Putty Session Generator
by d_jabsd (Acolyte) on Oct 25, 2004 at 16:26 UTC
    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!
      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 }

Log In?
Username:
Password:

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

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

    No recent polls found