Jerry Rocteur wrote:
>
> I'm trying to be a good boy and use strict and warnings ..

Well done!

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

Your problem is more than likely because you're expecting Perl to be
like another language. The usual ones are C and shell script. Perl's
syntax is often quite like C's, and the built-in functions are a fair
match, but unless you're used to declaring variables locally to their
scope of use as in C++ you will end up with a long preamble declaring
everything, which can be less than useful.

> 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.'
>
> Line 14 is the while line..

'use warnings' will moan at you if a variable's value is 'undefined'.
It's left this way straight after either

  my $var

or

  $var = undef

If you define stuff like:

  my $var = 0

then you won't get this problem. But don't go around initialising
all variables in their declarations. It's just a warning and is useful
as such, and anyway Perl will let you concatenate or increment an
undefined value. i.e.

  my $var;

followed by
  $var++;
or
  $var .= '>>'

will work fine without any warnings.

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

You need to get to understand Perl idioms. I've started by reducing your
indents from 8 chars to 2 because that makes it more visible for me.

> if ($#ARGV >= 0) {

This says 'if the index of the last array element is zero or more'.
You don't mean that, because an array with a single element will
have a last element with an index of zero. Try

  if (@ARGV) {
    :
  }

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

OK, these lines together pull the next argument from @ARGV and
quit the loop unless it begins with a hyphen. The reason you're
getting a warning is that once your 'shift' statement below has
finally emptied the array, $_ = $ARGV[0] will set $_ to 'undef'
so the pattern match is comparing against an undefined value.

This does the same:

  foreach (@ARGV) {

    last unless /^-/;

except that the parameters are left in @ARGV instead of being
shifted off, and the loop is executed just once for each
element of @ARGV.

>     last if /^--$/;

Break out if the element is just two hyphens. That's fine.

>     if (/^-d(.*)/) {
>       $debug++
>     }

Again, that'll do. But unless you need to capture whatever follows
the '-d' then you want just

     if (/^-d/) {
       $debug++
     }

>     elsif (/^-v(.*)/) {
>       $verbose++
>     }

Same here:

  elsif (/^-v/) {
    $verbose++
  }

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

And the rest's OK. But I'd write it something like this. (Actually I would
probably never write this, but my code depends on the context of the
code and what exactly you need to do with the results :)

  foreach (@ARGV) {

    next unless /^-/;

    last if $_ eq '--';

    if ( /^-(d|v)/ ) {
      $debug++ if $1 eq 'd';
      $verbose++ if $1 eq 'v';
    }
    else {
      print "$_ unknown option\n";
      Usage;
    }
  }

HTH,

Rob



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