On Dec 18, 2003, at 11:35 AM, Kenton Brede wrote: [..]
I've posted the following code to use as an example.  Any critique of
the code in addition to my specific question is welcome:)  I just don't
see a reason to stick an "else" statement on the end of that block.

http://www.nixnotes.org/perl_dump.html

Nice piece of code; clearly starting in the right general direction. I think I can see why you are asking about whether or not one should do an 'else' side of that tree.

So first things first, Do You REALLY want to
know what @ARGV was prior to calling GetOptions?

as a demonstration code:

        print "started with ARGS:\n";
        print ": $_  " foreach(@ARGV);
        print ":\n";
        my %options;
        GetOptions (\%options, "version", "help");
        
        while ( my ($k,$v) = each %options )
        {
                print "$k -> $v\n";
        }
        print "Ended with ARGS:\n";
        my @string = @ARGV;
        
        print ": $_ " foreach(@string);
        print ":\n";

[jeeves: 9:] ./get_opt_demo.plx -h bob fred --version
started with ARGS:
: -h  : bob  : fred  : --version  :
version -> 1
help -> 1
Ended with ARGS:
: bob : fred :
[jeeves: 10:]

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.

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.

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 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;
                ...

}

Which of course gets me to your

        sub get_record {
                my ($reg_ex, $file ) = @_;
                die "needed a regular expression"
                        unless($reg_ex);

                $file ||= "/etc/dhcpd.conf";
                 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...

HTH...

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to