Professional Web Applications Themes

Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14. - PERL Beginners

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

  1. #1

    Default Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

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

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

    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.

    #if ($#ARGV >= 0) { # What I've tried
    while ($_ = $ARGV[0], /^-/) {
    shift;
    last if /^--$/;
    if (/^-d(.*)/) { $debug++ }
    elsif (/^-v(.*)/) { $verbose++ }
    else { print "$_ unknown option\n";
    Usage;
    }
    }
    #}

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

    Jerry

    Jerry Rocteur Guest

  2. #2

    Default Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

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

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

    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.

    #if ($#ARGV >= 0) { # What I've tried
    while ($_ = $ARGV[0], /^-/) {
    shift;
    last if /^--$/;
    if (/^-d(.*)/) { $debug++ }
    elsif (/^-v(.*)/) { $verbose++ }
    else { print "$_ unknown option\n";
    Usage;
    }
    }
    #}

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

    Jerry

    Jerry Rocteur Guest

  3. #3

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

    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

    [url]http://danconia.org[/url]

    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_pr = new Getopt::Long::Pr ('config' =>
    ['gnu_compat','bundling','permute','no_getopt_compa t'] );


    # init configuration
    my $cmdopts = {};


    # get configuration
    $opt_pr->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



    Wiggins D'Anconia Guest

  4. #4

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

    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


    Rob Dixon Guest

  5. #5

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

    Wiggins d'Anconia wrote:
    > 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 $_?
    Thanks, Wiggins, and keep on rambling. The point needs reiteration. Aside from
    the needle-in-a-haystack problems, I'd say that it's a good rule of thumb:

    If you have to specifically assign $_, don't bother. Perl already assigns it in
    the contexts in which it is appropriate
    > 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...
    Yes and yes.
    >
    >
    > > #}
    > >
    > > 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 think about the only place it would be appropriate is in notes describing
    coding samples available for interview or included. If sufficiently understated,
    it may reassure an interviewer or screener that they are not wasting time:

    Codes samples included [all compiled using strict compilation option and
    pre-tested]

    Which at least indicates to the reader that they are looking at something which
    has run successfully. Definitely not something to wave a big flag over,
    though--it would be like listing:
    Have mastered ABCs
    ....at the top of a resume for an English professorship.

    Joseph

    R. Joseph Newton Guest

  6. #6

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.


    On Dec 13, 2003, at 10:01 AM, Wiggins d'Anconia wrote:
    [..
    > A decent template, any gurus have improvements?
    [..]

    my stock gag, looks like:

    my $opt = p_cmd_line_options();
    ....
    #---------------------------------------------------------------
    # These are the Subs for the p_cmd_line_options.
    #---------------------------------------------------------------
    # The Usage Command so that we can use a common interface to
    # handle both the cases where we want to show some basic detailed
    # information with a '--help' query, as well as to exit out of
    # the p_cmd_line_options wrapper for Getopt::Long if we have
    # problems with user generated commands.
    #---------------------------------------------------------------

    sub Usage
    {
    my($msg, $retval, list) = _;

    print "$msg\n" if ($msg);

    print "Usage: $0 [-i|-x|-c|--kadb] [-f <alt_file>] [-d
    <sysname>]\n";
    if ( list ) {
    print " the full optins list is:\n";
    print "\t$_\n" foreach (list);
    }
    exit($retval);
    }

    #------------------------
    # where we wrap the Getopt::Long and resolve which arguments
    # work and play well with other command line arguments.
    #
    # <editorialize on the what this version does or does not do
    #
    sub p_cmd_line_options
    {
    my $ref = {};

    $ref->{'delete'} = [];
    #
    # more editorializing
    #
    $ref->{'add'} = [] ;

    #
    # this also doubles as the flash guide. in Usage
    #
    my list = qw/
    file|f=s
    import|init|i
    installdb
    export|x|e
    compact|c
    delete|d=s
    add|a=s
    fsck
    nup
    kadb
    help|h|?/;

    my config_args = qw/bundling/;
    Getopt::Long::Configure(config_args);

    my $results = GetOptions($ref, list) ;

    Usage('Problem with Input Arguments',1,list) unless ($results);

    Usage('The General Help',0,list) if ($ref->{'help'});

    #
    # now to create the action station section
    #

    #sort out other gory details about the which arguments
    #should or should not be dealt with

    $ref ;

    } # end of p_cmd_line_options


    This dog has soldiered on in one variant or another...

    vladimir: 41:] ./db_tool.plx --kadb
    option: help
    we have the following options:
    add [sysname config_host] -> add sysname and config_host to db
    delete [sysname ] -> delete sysname from db
    show_sysnames -> list current sysnames in db
    config_host -> get config_host for sysname in db
    export -> export db to file
    import -> import file to db
    q|exit|quit -> exit kadb

    option: q
    exiting
    vladimir: 42:]

    the '--kadb' started out as a bad JOKE, I whined at
    myMostGloriousLeader that just for him, I would offer
    up a 'kryptic command line interface with short and
    arcane syntax, you know, just like kadb....' IT
    HURTS when one stops laughing and has to implement
    it in a rush because, well, one just needs it down and dirty,
    but it has to work...

    I went with the 'bundling' approach so that the
    'oldGuy' *nix freaks would be 'happy' while the
    gnuSters would be able to wander the plains with
    their --help and the like...

    ciao
    drieux

    ---

    Drieux Guest

  7. #7

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.


    On Saturday, Dec 13, 2003, at 18:12 Europe/Brussels, 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 ;-((
    >
    > 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..
    >
    > 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.
    >
    > #if ($#ARGV >= 0) { # What I've tried
    > while ($_ = $ARGV[0], /^-/) {
    > shift;
    > last if /^--$/;
    > if (/^-d(.*)/) { $debug++ }
    > elsif (/^-v(.*)/) { $verbose++ }
    > else { print "$_ unknown option\n";
    > Usage;
    > }
    > }
    > #}
    >
    > Thanks in advance, I really want to put, Perl with warnings and strict
    > in my CV ;-))))))))))))
    >
    > Jerry
    >
    >

    Sorry for the double post, my first post I had done with an email
    address that is not subscribed to the list, thinking it would not go
    through I reposted. I didn't realize you accepted posts from non
    subscribed addresses.

    I thank all those that replied on and off list, great replies. Of
    course my question was more than answered. Thanks to Wigins d'Anconia,
    Rob Dixon, R. Joseph Newton and drieux! Food for thought. I'm still
    reviewing your replies..

    Best Regards,

    Jerry Rocteur

    Jerry Rocteur Guest

  8. #8

    Default RE: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

    > uninitialized value in
    > pattern match (m//) at ./getopt.pl line 14.'
    Use the standard Getopt::Std module to process options. Don't do it
    yourself.
    > 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.
    >
    > #if ($#ARGV >= 0) { # What I've tried
    > while ($_ = $ARGV[0], /^-/) {
    > shift;
    > last if /^--$/;
    > if (/^-d(.*)/) { $debug++ }
    > elsif (/^-v(.*)/) { $verbose++ }
    > else { print "$_ unknown option\n";
    > Usage;
    > }
    > }
    > #}
    Luke Bakken Guest

  9. #9

    Default Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.

    On Sat, 13 Dec 2003 18:12:17 +0100
    Jerry Rocteur <rocteurmac.com> wrote:
    > 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'

    Because so many people in c.l.p.m said "use strict;" I decided to take the plunge and use it.

    Until I worked out what was going on, I, like you, thought it was a waste of time.

    That and warnings are a great pair in getting programs to run and I suppose "Use of unitialized..." is the most common warning. There is always a bit of pleasure in tracking down the reason for that one.

    See [url]http://perl.abigail.nl/Musings/coding_guidelines.html[/url] suggested perl coding practices

    Hang on in there, it's worth it.




    --
    Owen

    Owen Guest

Similar Threads

  1. pattern match
    By Michel in forum PHP Development
    Replies: 23
    Last Post: December 12th, 12:55 PM
  2. please help !! pattern match
    By Pandey Rajeev-A19514 in forum PERL Beginners
    Replies: 0
    Last Post: September 5th, 01:18 PM
  3. Return second line after a pattern match?
    By scriptOmatic in forum AIX
    Replies: 1
    Last Post: July 29th, 07:47 PM
  4. uninitialized value in pattern match
    By gilgames in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 13th, 07:26 PM
  5. Pattern match with 2 conditions
    By Andrew Perrin in forum Perl / CGI
    Replies: 2
    Last Post: June 27th, 09:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139