On Thu, Dec 18, 2003 at 12:37:33PM -0800, drieux wrote: > > On Dec 18, 2003, at 11:35 AM, Kenton Brede wrote: > http://nixnotes.org/perl_dump.html
Thanks for everyones response first of all. > So first things first, Do You REALLY want to > know what @ARGV was prior to calling GetOptions? Point well taken and I have modified the code to reflect that. > > I take us on this misadventure to recommend my traditional > approach of basically Hiding the 'parse command line options' > in function of it's own that will resolve if all the > required things are there, and returning only > what needs to be dealt with, since the code is now Good to Go. > I've worked with the examples you have shown and I have to admit I'm just not advanced enough to follow your approach. I'll ask a few questions in hopes of getting a little closer to understanding. > say something on the order of > > my ($argv, $options) = parse_cmd_line(); > where of course $argv is an array ref and $options is > a hash ref of stuff that will be used in the code. > In the context of my example would $argv and $options be something like $string[0] and $option{version} ? > This way down in that > > sub parse_cmd_line > { > ... > if ( $options{version} && $options{help} ) > { > print_version('help'); # tell print_version to call > help and exit > } elsif ( $options{help} or scalar(@ARGV) != 1 ) > { > # in the case we ONLY want one argument > help_message(); > } elsif ( $options{version}) > { > print_version(); # just the version string > } .... > > ([EMAIL PROTECTED],\%options); > } > I don't understand using " print_version('help'); "? Why not just call " help_message "? You have the comment "# tell print-version to call help and exit" How by sending the string 'help' in print_version() is that done? > I of course implement your help_message() function with > the ability to take an argument like > > sub help_message > { > my ($whiney_msg) = @_; > > print $whiney_msg if $whiney_msg; > ... > > } > I don't understand the reason for passing an argument to help_message() and the reason for the conditionals. In the example I provided I would have to call it like this - help_message(' print <<EOF; $0: search dhcpd.conf records by: IP, MAC, host, user. Usage: $0 SEARCH_STRING $0 [OPTION] -v, --version Version -h, --help Help EOF exit; '); and then pass the text to the @_ variable, which is passed to $whiney_msg and then if there is a $whiney_msg then print the $whiney_msg. Seems like a lot of work to just print the help_message() directly. I probably just don't understand your overall structure. > Which of course gets me to your > So I could pass the reg_ex like so - get_record($reg_ex[0]); > sub get_record { > my ($reg_ex, $file ) = @_; $reg_ex slurps in the value passed to it and $file contains the file passed from below? At least that is what appears to happen when I run the code. > die "needed a regular expression" > unless($reg_ex); > > $file ||= "/etc/dhcpd.conf"; ^^ I've seen || as the or operator but haven't found and example of ||= yet. What does that do exactly? > open (DHCPD, $file) || die "Can't open $file : $!\n"; > $/ = '}'; # paragraph delimiter > while ( <DHCPD> ) { > if (m/$reg_ex/i) {print "$_\n\n"}; > } > close (DHCPD) || die "Error closing /etc/dhcpd $!\n"; > } > > This way you move towards reading IN the RegEx that > is passed to the function - and can pass in a 'test file' > for the duration of hacking out the test context... > I don't really understand what you mean here. Are you saying some part of the code above somehow makes the development process easier and then parts should be discarded? Sorry for all the questions but I would like to understand. Thanks, Kent -- "I am always doing that which I can not do, in order that I may learn how to do it." --Pablo Picasso -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>