Jerry Rocteur wrote:
Hi,

I'm trying to be a good boy and use strict and warnings ..

The more I do, the more I feel I'm wasting so much time and should become productive, my code looks full of 'my', I could understand the requirement inside a sub routing but in the main code it gives me the willies. My DBI Perl is even more of a headache ;-((


Right now you probably are. In the future the loads of time it saves will repay you greatly. Stick with it, it will help... this comes from someone that switched "cold turkey" from not using them to using them after coding in Perl for 3+ years...


Anyway, enough whining, now I've going through code that is working and thought I'd see the big difference if I change it to use warnings and strict and this little routing gives me 'Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.'


The above is just a warning so your code is running, granted it doesn't do much...


Line 14 is the while line..

I've tried all sorts of stuff with defined but keep getting syntax errors. I've tried perldoc warnings and perldoc perllexwarn .. In any case, unless I'm really missing something, I can't see what could be problematic with the while statement below, of course this shows my ignorance but as I've decided to use warnings and strict, I want to do it right..

How would a pedant code the following to avoid this warning.. Your answers will help me in the future.


They wouldn't. They would use Getopt::Std or Getopt::Long...


perldoc Getopt::Std
perldoc Getopt::Long

Now....

#if ($#ARGV >= 0) { # What I've tried

To test if an array has length simply take it in scalar context,


if (@ARGV) {

while ($_ = $ARGV[0], /^-/) {

I believe the issue is that the $_ is not set by the time you hit the pattern match, there is probably a way to make it, but to me it is simpler to just add a line after the initial test such as:


next unless (/^-/);

Inside the loop and drop the test inside the conditional.

                shift;
                last if /^--$/;
                if (/^-d(.*)/) { $debug++ }
                elsif (/^-v(.*)/) { $verbose++ }

I assume that $debug and $verbose have been pre-declared and that you have Usage prototyped or something, as it is a bare word...


                else { print "$_ unknown option\n";
                        Usage;
                }
        }

More code showing specifically why I have been rambling about $_ in the last couple of days. If you are going to shift off the argument list why not just do it in the while loop when you are setting $_? And Perl is very nice and lets you leave the semi-colons off in the above, to me it is a bad style to get into and will likely lead to errors later when you (or someone else) decides to add additional statements to those blocks. While you are a beginner code *exactly* what you mean, use lots of names, avoid the shortcuts, later when you don't need to ask these kinds of questions, then sign up for a golf tournament...


#}

Thanks in advance, I really want to put, Perl with warnings and strict in my CV ;-))))))))))))


I have wondered about this for a while, putting that you use warnings and strict into a CV is a tough choice, to me I wouldn't likely talk to a candidate unless they used them, but I am not sure I would put them on a CV because to an advanced person it would seem overally obvious. Though I think it is something good to bring up in an interview, and I have had several interviewers ask about them... what does the group think (about including it in a CV)? (Obviously I know what the thoughts are about including them in the code)....And naturally code samples would reflect their use.


I would also check out:

perldoc Pod::Usage

http://danconia.org

A decent template, any gurus have improvements?

UNTESTED---

#!/usr/local/bin/perl
use strict;
use warnings;


use Getopt::Long; use Pod::Usage;


pod2usage("No options specified\n") unless (@ARGV);





# configure GetOptions with some standard gnu options
my $opt_parser = new Getopt::Long::Parser ('config' => ['gnu_compat','bundling','permute','no_getopt_compat'] );



# init configuration my $cmdopts = {};


# get configuration $opt_parser->getoptions($cmdopts, 'help|h', 'debug|d', 'verbose|v', ) or pod2usage(2);


# print the help message if they asked just for it pod2usage(1) if (defined($cmdopts->{'help'}));


if ($cmdopts->{'verbose'}) { print "Verbose Mode\n"; } if ($cmdopts->{'debug'}) { print "Debugging Mode\n"; }




############################################################################# __END__


=head1 NAME



getopt - A program for parsing command line arguments



=head1 SYNOPSIS



getopt [options]



Options: --debug turn on debugging --verbose turn on verbosity --help print this message


=head1 OPTIONS



=over 8



=item B<--debug>



Mode of operation resulting in debugging statements



=item B<--verbose>



Mode of operation resulting in lots of messages



=item B<--help>



Print a brief help message and exit.



=back



=head1 DESCRIPTION



My first program



=cut





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