From jhorner at 2jnetworks.com Mon Jan 14 18:14:50 2002 From: jhorner at 2jnetworks.com (J. J. Horner) Date: Thu Aug 5 00:05:29 2004 Subject: List still active? Message-ID: <20020114191450.A1348@2jnetworks.com> Hello? Anyone home? JJ -- J. J. Horner "H*","6a686f726e657240326a6e6574776f726b732e636f6d" *************************************************** "H*","6a6a686f726e65724062656c6c736f7574682e6e6574" Freedom is an all-or-nothing proposition: either we are completely free, or we are subjects of a tyrannical system. If we lose one freedom in a thousand, we become completely subjugated. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 240 bytes Desc: not available Url : http://mail.pm.org/archives/knoxville-pm/attachments/20020114/88d76eda/attachment.bin From kguidry at utk.edu Fri Jan 18 15:22:35 2002 From: kguidry at utk.edu (Kevin Guidry) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique Message-ID: Hello all. Would it be permissable and appropriate to post some code here on the list for review and critique? I'm not much of a coder, so I'd just like some feedback on a short program. I know it works, but I'd like to know if it works well, is organized intelligibly and intelligently, etc. I also know that there are many ways to do the same thing in perl and I'd like to know if the decisions I made were the right ones. Thanks for your time. If you have Monday off, enjoy the three day weekend. I'm sure that everyone else is jealous. :) Kevin http://knoxville.pm.org/ From anthony at edge.net Fri Jan 18 10:49:47 2002 From: anthony at edge.net (Anthony Chatman) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique References: Message-ID: <3C48522B.3010700@edge.net> Sure, just as long as it wont be burden on those who are bandwidth deprived. Kevin Guidry wrote: > Hello all. Would it be permissable and appropriate to post some > code here on the list for review and critique? I'm not much of a coder, > so I'd just like some feedback on a short program. I know it works, but > I'd like to know if it works well, is organized intelligibly and > intelligently, etc. I also know that there are many ways to do the same > thing in perl and I'd like to know if the decisions I made were the right > ones. > Thanks for your time. If you have Monday off, enjoy the three day > weekend. I'm sure that everyone else is jealous. :) > > > Kevin > > http://knoxville.pm.org/ > > > http://knoxville.pm.org/ From kguidry at utk.edu Fri Jan 18 16:37:09 2002 From: kguidry at utk.edu (Kevin Guidry) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique In-Reply-To: <3C48522B.3010700@edge.net> Message-ID: Much thanks in advance. The code is, in its present form, just a method of getting three pieces of information from a user at the console, inserting them into a command, and then exec'ing that command. The specific pieces of info are IP address, subnet mask, and gateway. I do a quick check to make sure that they all "look" like IP addresses before passing them to the command. The specific command is a Windows 2000 command "netsh" which allows, among other things, you to change your network settings from the command line. In this case, I am just changing the three afore mentioned items on a laptop that will be moved around quite a bit. My sincere apologies if this is too long or not understandable; please bear with me as I learn. Without further ado, here it is: use strict; my $ip = 0.0.0.0; my $netmask = 0.0.0.0; my $gateway = 0.0.0.0; my $validip = 0; my $digit; my @digits; sub checkip; print "Enter the IP address: "; $ip = ; chomp($ip); if(!checkip($ip)){ # Crap code follows # I want to user to see the error message, so I have to pause for a keystroke # since this will be run from the desktop and not a terminal print "\n$ip is not a valid IP address\n"; print "\n\nPlease press enter..."; $ip = ; die "$ip is not a valid IP address"; } print "Enter the subnet mask: "; $netmask = ; chomp($netmask); if(!checkip($netmask)){ # More of the same crap code print "\n$netmask is not a valid subnet mask\n"; print "\n\nPlease press enter..."; $ip = ; die "$netmask is not a valid subnet mask"; } print "Enter the default gateway: "; $gateway = ; chomp($gateway); if(!checkip($gateway)){ # More of the same crap code print "\n$gateway is not a valid IP address for a gateway\n"; print "\n\nPlease press enter..."; $ip = ; die "$gateway is not a valid IP address for a gateway\n"; } #exec "netsh interface ip set address \"Local Area Connection\" static $ip $netmask $gateway 1\n"; print "netsh interface ip set address \"Local Area Connection\" static $ip $netmask $gateway 1\n"; # Another instance of crap code to pause for the user to read print "\n\n\nPlease press enter..."; $ip = ; # checkip checks the one variable passed in to see if it's a valid IP address # If returns a 1 on success and a 0 on failure sub checkip{ my $ip_being_checked = @_[0]; if ($ip_being_checked =~ /(\d+)(\.\d+){3}/){ $validip = 1; } if($validip){ @digits = split(/\./, $ip_being_checked); foreach $digit (@digits){ if ($digit < 0 || $digit > 255){ $validip = 0; last; } } } if ($validip == "0"){ return 0; } else{ return 1; } } http://knoxville.pm.org/ From sml at zfx.com Fri Jan 18 18:16:59 2002 From: sml at zfx.com (Steve Lane) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique References: Message-ID: <3C48BAFB.D98D6660@zfx.com> Kevin Guidry wrote: > Hello all. Would it be permissable and appropriate to post some > code here on the list for review and critique? i think this is always appropriate and welcome for lists such as this one. > I'm not much of a coder, > so I'd just like some feedback on a short program. I know it works, but > I'd like to know if it works well, is organized intelligibly and > intelligently, etc. I also know that there are many ways to do the same > thing in perl and I'd like to know if the decisions I made were the right > ones. Kevin Guidry wrote: > The code is, in its present form, just a method of getting three > pieces of information from a user at the console, inserting them > into a command, and then exec'ing that command. The specific pieces of > info are IP address, subnet mask, and gateway. I do a quick check to make > sure that they all "look" like IP addresses before passing them to the > command. The specific command is a Windows 2000 command "netsh" which > allows, among other things, you to change your network settings from the > command line. In this case, I am just changing the three afore mentioned > items on a laptop that will be moved around quite a bit. > My sincere apologies if this is too long or not > understandable; please bear with me as I learn. Without further ado, here > it is: as far as working goes... if someone enters a non-IP, your program sort of implies that the user gets another chance to enter one, but they don't. the program die's whatever the user does. so... here's my version of your program. it does the same thing (except with retry-input until good IP), but i pulled the input stuff into a sub, and put in a couple of common Perl idioms. #!/usr/bin/perl -w use strict; my $ip = get_ip( "Enter the IP address: " ); my $netmask = get_ip( "Enter the subnet mask: " ); my $gateway = get_ip( "Enter the default gateway: "); #exec qq[netsh interface ip set address "Local Area Connection" static $ip $netmask $gateway 1\n]; print qq[netsh interface ip set address "Local Area Connection" static $ip $netmask $gateway 1\n]; exit; ### SUBS # the three requests for input are the same # except for the prompt, so make a sub out of them sub get_ip { # given a prompt, shows the prompt and # asks user to enter IP address. # re-prompts if IP is not valid. my $prompt = shift; my $ip; GET_IP: { # this starts a block, for the "redo" and "last" later. print $prompt; # these next two are often combined, into: # "chomp( my $ip = );" $ip = ; chomp $ip; last GET_IP if checkip( $ip ); warn "'$ip' is not a valid IP address. Please try again.\n"; redo GET_IP; } return $ip; } sub checkip { # confirm that input is a valid IP address. # return 1 if so, 0 if not. my $ip = shift; my @quads = split /\./, $ip; # confirm that there are 4 quads, # and each quad is an integer, # and each integer is 0-255. my $QUADS_IN_IP = 4; return 0 unless $QUADS_IN_IP == grep { /^\d+$/ and $_ >= 0 and $_ <= 255 } @quads; # looks like a good ip. return 1; } __END__ [sml@gull ~/bin]$ ./kevin-guidry-mine Enter the IP address: 45.100.26.1 Enter the subnet mask: 255.300.255.128 '255.300.255.128' is not a valid IP address. Please try again. Enter the subnet mask: 48 '48' is not a valid IP address. Please try again. Enter the subnet mask: 255.255.255.128 Enter the default gateway: 45.100.26.1 netsh interface ip set address "Local Area Connection" static 45.100.26.1 255.255.255.128 45.100.26.1 1 -- Steve Lane http://knoxville.pm.org/ From jhorner at 2jnetworks.com Fri Jan 18 22:38:09 2002 From: jhorner at 2jnetworks.com (J. J. Horner) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique In-Reply-To: ; from kguidry@utk.edu on Fri, Jan 18, 2002 at 05:37:09PM -0500 References: <3C48522B.3010700@edge.net> Message-ID: <20020118233808.A12375@2jnetworks.com> * Kevin Guidry (kguidry@utk.edu) [020118 17:41]: > Much thanks in advance. > > The code is, in its present form, just a method of getting three > pieces of information from a user at the console, inserting them > into a command, and then exec'ing that command. The specific pieces of > info are IP address, subnet mask, and gateway. I do a quick check to make > sure that they all "look" like IP addresses before passing them to the > command. The specific command is a Windows 2000 command "netsh" which > allows, among other things, you to change your network settings from the > command line. In this case, I am just changing the three afore mentioned > items on a laptop that will be moved around quite a bit. A couple of notes: 1. Look up Regexp::Common on CPAN. Good basic regular expressions to check an number of things, in this case, the IP. Usage #!/bin/perl use Regexp::Common; while() { if(/$RE{net}{IPv4}{december}{-keep}/) { print "IP Address: $1\n"; } } __DATA__ 24.113.50.245 0.42.523.2 255.242.52.4 2.5.3 2. It is okay to use any effective method to check user input, as long as it is readable, maintainable, and reasonably efficient. I often do the same thing as you, but instead of just printing out a message, I may create a has with different messages assigned to numbers, then as my subroutines finish, I have them return a status code that matches one of the keys in the hash. I then just print out the code. If you end up writing larges amounts of code, and you find this method helps you in some way, use it. 3. One thing that may help your code: BUY THE PERL CD BOOKSHELF!! Honestly, for ~$60.00 it was the best investment I ever made. The version I got came with: - Learning Perl - Learning Perl on Win32 - Perl Cookbook (a must have, if you ask me) - Advanced Perl Programming - Programming Perl - Perl in a Nutshell. I hear the newer version comes with Perl for the SysAdmin. Must be cool. I just copied content over to my webserver and now I have an online reference from anywhere in the world. Just a simple login and I'm golden! 4. A good place to get help and view good code, as well as learn more information: http://www.perlmonks.org/ . Randal Schwartz hangs out there occasionally. If you have any more questions, let me know. Thanks JJ -- J. J. Horner "H*","6a686f726e657240326a6e6574776f726b732e636f6d" *************************************************** "H*","6a6a686f726e65724062656c6c736f7574682e6e6574" Freedom is an all-or-nothing proposition: either we are completely free, or we are subjects of a tyrannical system. If we lose one freedom in a thousand, we become completely subjugated. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 240 bytes Desc: not available Url : http://mail.pm.org/archives/knoxville-pm/attachments/20020118/31e665ce/attachment.bin From fike at cs.utk.edu Sat Jan 19 08:44:39 2002 From: fike at cs.utk.edu (Don) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique In-Reply-To: <20020118233808.A12375@2jnetworks.com> Message-ID: Nice, I like Steve's layout and I had never used Regexp::Common. I learned from both replies and this wasn't even my question. Thanks for asking Kevin, I think the list has been waiting for you... Don http://knoxville.pm.org/ From kguidry at utk.edu Mon Jan 21 16:04:22 2002 From: kguidry at utk.edu (Kevin Guidry) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique In-Reply-To: Message-ID: On Sat, 19 Jan 2002, Don wrote: >Nice, I like Steve's layout and I had never used Regexp::Common. Me, too. Your layout, Steve, got me thinking about how to restructure the whole program to reduce reduncancy and make it easier to read. I also have a feeling that I'll be digging through Regexp::Common to see what other wheels I won't need to reinvent. Question: Where do ya'll turn when you have to, uh, do something new in Perl, something that you've never done before? What kinds of resources do you regularly turn to before asking questions? Someone mentioned O'Reilly's Perl Bookshelf, which most of us would agree is a very good resource(although I question its utility as an introduction to Perl). If it hasn't already been done(and it may have been; please be gentle with me I'm just repeating history) it may be worth our while to contribute suggestions so that we can gather up a list of *our* favorite and most-used resources. I'd be happy to serve as a collection agent of sorts to gather them from everyone. On a related note, does anyone have the new Perl Bookshelf? I saw that they dropped both Learning Perl and Learning Perl on Win32 and added a new Perl for Sysadmins. Is it worth the money for those that already have the first one? Finally, I'll be finishing up that piece of code that I submitted for "review" probably this afternoon. If anyone would like to see the final product and maybe offer some suggestions or just want to make sure that I actually followed your suggestions, let me know. I'll be happy to send the code your way, but I don't think it necessary to clutter up the list again. Kevin http://knoxville.pm.org/ From sml at zfx.com Mon Jan 21 18:28:22 2002 From: sml at zfx.com (Steve Lane) Date: Thu Aug 5 00:05:29 2004 Subject: Code critique References: Message-ID: <3C4CB226.C19680E2@zfx.com> Kevin Guidry wrote: > >Nice, I like Steve's layout and I had never used Regexp::Common. > > Me, too. Your layout, Steve, got me thinking about how to > restructure the whole program to reduce reduncancy and make it easier to > read. I also have a feeling that I'll be digging through Regexp::Common > to see what other wheels I won't need to reinvent. i haven't used Regexp::Common either, though that's no excuse as i've been told about it many times! so yeah i would favor using that, too. > Question: Where do ya'll turn when you have to, uh, do something > new in Perl, something that you've never done before? i usually turn to places like this. when i was cutting my teeth on Perl, the Waite Group had an excellent perl-beginners list to accompany its Jon Orwant Perl book. that list is not around anymore, so i often mail whichever .pm group list i'm on that has been most active lately. lots of people talk about Perlmonks... so be sure to check it out. i haven't delved into that site yet myself. and having a good book or two that you trust is very valuable. my most-often used Perl book by far is the Perl Cookbook. it's not often that you'll have a task to do that isn't clearly illustrated in the Cookbook. Joseph Hall's book, Effective Perl Programming, is a wonderful book to just curl up with and let soak in. both of those books teach not only "tricks" in Perl, but the common Perl idioms. and i feel that the key step on the way to mastering Perl is learning its common idioms. there is a perl-beginners mailing list. i wasn't too impressed with it the last time i looked; it had a lot of noise, and too many truly dumb questions. find it and many many other Perl lists at http://lists.perl.org . all that said... ask 10 Perl programmers this question and you'll likely get 10 different answers. i hope that you and others use knoxville-pm-list as a first go, though. this particular .pm group probably has the most potential of the "mostly-quiet" .pm groups that i'm familiar with. this list has a whole lot of very bright subscribers; they just need more to talk about! :) -- Steve Lane http://knoxville.pm.org/ From rdicaire at ardynet.com Sun Jan 27 11:12:22 2002 From: rdicaire at ardynet.com (R Dicaire) Date: Thu Aug 5 00:05:29 2004 Subject: arrays Message-ID: <3C5434F6.3EA63E41@ardynet.com> Hi, I've been lurking on the list since last summer, and have a question. I'm trying to get the contents of 'uname -a' into an array. I have: #/usr/bin/perl @a=`uname -a`; print "$a[0] $a[1] $a[10]"; But this isn't working as I thought it would, its not printing the selected array elements. Any ideas? http://knoxville.pm.org/ From sml at zfx.com Sun Jan 27 13:52:49 2002 From: sml at zfx.com (Steve Lane) Date: Thu Aug 5 00:05:29 2004 Subject: arrays References: <3C5434F6.3EA63E41@ardynet.com> Message-ID: <3C545A91.8128737@zfx.com> R Dicaire wrote: > Hi, I've been lurking on the list since last summer, and have a > question. > I'm trying to get the contents of 'uname -a' into an array. > I have: > > #/usr/bin/perl > @a=`uname -a`; > print "$a[0] $a[1] $a[10]"; > > But this isn't working as I thought it would, its not printing the > selected > array elements. Any ideas? when you use backticks in list context, they return a list of -lines- that the external command printed. i think you are expecting for @a to contain a list of -words- that `uname -a` returns. since `uname -a` prints only one line, that entire line goes into $a[0], and so your print statement prints the entire `uname -a` result. to get words, you need to first grab the line that `uname -a` prints, and then split it into words. try this: #/usr/bin/perl -w use strict; my $uname = `uname -a`; chomp $uname; my @words = split / /, $uname; print "OS( $words[0] ) HOSTNAME( $words[1] ) ARCH( $words[10] )\n"; __END__ for more, see: man perlop => qx/STRING/ or `STRING` perldoc -f split -- Steve Lane http://knoxville.pm.org/