metaperl has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: improov mein k0den
by jdporter (Paladin) on Mar 29, 2007 at 15:13 UTC | |
Well, you're clearly a C programmer, since you use printf unnecessarily and you use the C-style for loop. :-) I'd say you've done a fairly conventional procedural decomposition of the task. You have four or five subroutines, but they're very task specific (that is, they're not resuable) because they either have hard-coded values or they share a dependency on some external data. One good solution in cases like this is to go object oriented. But that's not the only possible approach. My preference, at least for something this simple, would be to genericize the subroutines to eliminate their hard-coded data and their dependency on external variables, making all that stuff parameters instead. And really, there's no reason for so many subroutines. One will do.
I eliminated your printf and C-style for loop. I also fixed a bug: testing of the user input should first check that the string is a number before doing numeric tests on it. Also, by unsetting @ARGV, I ensure that input is from the console. You may or may not want this.
A word spoken in Mind will reach its own level, in the objective world, by its own weight
| [reply] [d/l] |
by jettero (Monsignor) on Mar 29, 2007 at 18:59 UTC | |
Just kindof a side note, as I'm sure it's common knowledge. The following won't run on modern linuxes.
It's considered a security problem to have a non-absolute path in a she-bang. The usual way around it is #!/usr/bin/env perl. -Paul | [reply] [d/l] [select] |
|
Re: improov mein k0den
by Fletch (Bishop) on Mar 29, 2007 at 15:37 UTC | |
Well . . . (gah, this almost got eaten by the server hiccups) | [reply] [d/l] [select] |
|
Re: improov mein k0den
by davorg (Chancellor) on Mar 29, 2007 at 14:41 UTC | |
-- See the Copyright notice on my home node. "The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg | [reply] |
by jdporter (Paladin) on Mar 29, 2007 at 16:44 UTC | |
Nice idea, but... Watch what happens on Windows*:
A check of the IO::Prompt man page splains that this means your environment has no /dev/tty available, in which case there isn't much you can do with this module. Sorry.Diving into the module's source, we find that it is indeed rife with the following: And yet, inexplicably (ironically?), the documentation's DEPENDENCIES, INCOMPATIBILITIES, and BUGS AND LIMITATIONS sections are coyly silent on this shameless OS bondage. Somehow this doesn't strike me as "perl best practices". Very disappointing. * Not just Windows, but any other OS which doesn't represent its terminal in the filesystem via a file named /dev/tty. Which is most of them.
A word spoken in Mind will reach its own level, in the objective world, by its own weight
| [reply] [d/l] [select] |
| |
|
Re: improov mein k0den
by saintly (Scribe) on Mar 29, 2007 at 15:42 UTC | |
The program could be made a lot simpler by combining functions, etc..., but thats not necessarily an advantage from the long term maintenance perspective. On the other hand, the program was so simple that it's not going to be much of a headache to maintain in the future as-is. The changes I suggested are what I'd suggest to a novice Perl programmer who turned in the above code for deployment on a production system. | [reply] [d/l] |
by TGI (Parson) on Mar 29, 2007 at 19:48 UTC | |
Better yet why not change the lookup function to index from 1 and prevent accidentally mucking about with HOSTS.
Now you can simplify choose_host.
The concatenation in ssh_string is hard to read, try join with a null string for assembling the return value:
Also, ditch the unneeded & sigil from your subroutine calls. It can have unexpected consequences. See perlsub. Update: The relevent bits from perlsub, with emphasis added: ...If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.
| [reply] [d/l] [select] |