Help with uninitialized value warning

Discussion in 'Perl Misc' started by Jon, Sep 17, 2005.

  1. Jon

    Jon Guest

    I am running into a problem with the following snippet of code:

    my $job = "";
    my $db = "";
    my @events;
    my @pieces;
    my $start;
    my $running;
    my %hash;
    my $event;
    my @input = `autorep -J PVS%R -d`;

    foreach (@input) {
    if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
    if($1 ne $job || $2 ne $db) {
    undef @events;
    }
    $job = $1;
    $db = $2;
    }

    When the script first runs, I get the "Use of uninitialized value in
    string eq" at the line with the regex. I do know that initially, the
    input does not match. However, after the first iteration through the
    foreach loop, I get no other warnings even if the input doesn't match.
    Can anyone tell me what is uninitialized? Thanks.
    Jon, Sep 17, 2005
    #1
    1. Advertising

  2. Jon

    Paul Lalli Guest

    > I am running into a problem with the following snippet of code:
    >
    > my $job = "";
    > my $db = "";
    > my @events;
    > my @pieces;
    > my $start;
    > my $running;
    > my %hash;
    > my $event;
    > my @input = `autorep -J PVS%R -d`;
    >
    > foreach (@input) {
    > if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
    > if($1 ne $job || $2 ne $db) {
    > undef @events;
    > }
    > $job = $1;
    > $db = $2;
    > }
    >
    > When the script first runs, I get the "Use of uninitialized value in
    > string eq" at the line with the regex.


    I find that to be astonishingly unlikely. You have no 'eq' operator
    anywhere in this code. Please read the posting guidelines for this
    group, and then follow their suggestions - post a short but complete
    script that *we can run* by copy and pasting. Since it's possible we
    don't have the "autorep" program, we have no way of knowing what is
    contained in @input. Create a test script that populates @input
    directly, and then post it here.

    > I do know that initially, the
    > input does not match. However, after the first iteration through the
    > foreach loop, I get no other warnings even if the input doesn't match.
    > Can anyone tell me what is uninitialized?


    When I run this script, I get no warnings and no output of any kind.
    Of course, that's likely because @input is empty for me. Please post a
    script we can run.

    Paul Lalli
    Paul Lalli, Sep 17, 2005
    #2
    1. Advertising

  3. Jon

    Matt Garrish Guest

    "Jon" <> wrote in message
    news:...
    >I am running into a problem with the following snippet of code:
    >
    > my $job = "";
    > my $db = "";
    > my @events;
    > my @pieces;
    > my $start;
    > my $running;
    > my %hash;
    > my $event;
    > my @input = `autorep -J PVS%R -d`;
    >
    > foreach (@input) {
    > if(/(PV.*)\_([A-Z]?[A-Z0-9])/) {
    > if($1 ne $job || $2 ne $db) {
    > undef @events;
    > }
    > $job = $1;
    > $db = $2;
    > }
    >
    > When the script first runs, I get the "Use of uninitialized value in
    > string eq" at the line with the regex.


    I see two instances of 'ne', but none of 'eq', so you haven't posted enough
    code to find the problem (or worse, you changed your code).

    In general, unintialized warnings simply alert you that you're trying to use
    a variable that hasn't yet had a value assigned to it (or has been assigned
    the undef value), which is often not what you want. For example:

    my $job = '';
    my $db;

    if ($job eq $db) {
    print 'the above comparison will generate a warning';
    }

    If you find it odd that the comparison succeeds, it's because an undef value
    is equal to an empty string. (I thought this behaviour was defined in
    perlop, but it appears not. Is it just assumed that people will know, or
    have I just forgotten where the reference is? I expect the latter...)

    Matt
    Matt Garrish, Sep 17, 2005
    #3
  4. Jon

    Guest

    Matt Garrish wrote:
    > "Jon" <> wrote in message
    > news:...
    > >I am running into a problem with the following snippet of code:
    > >

    > omitted...
    >
    > If you find it odd that the comparison succeeds, it's because an undef value
    > is equal to an empty string. (I thought this behaviour was defined in
    > perlop, but it appears not. Is it just assumed that people will know, or
    > have I just forgotten where the reference is? I expect the latter...)
    >


    The ancient Camel spells it out in "Gory Details" but, apparently, got
    omitted in the latest:

    There are actually scalars that have neither a numeric nor a string
    value,
    and these we call "undefined". An undefined scalar evaluates to 0
    in a
    numeric context and to the null string in a string context...

    --
    Charles DeRykus
    , Sep 18, 2005
    #4
  5. Jon

    Paul Lalli Guest

    Matt Garrish wrote:

    > an undef value
    > is equal to an empty string. (I thought this behaviour was defined in
    > perlop, but it appears not. Is it just assumed that people will know, or
    > have I just forgotten where the reference is? I expect the latter...)


    perldoc perlsyn
    A variable holds the undefined
    value ("undef") until it has been assigned a defined value,
    which is anything other than "undef". When used as a
    number, "undef" is treated as "0"; when used as a string, it
    is treated the empty string, """"; and when used as a
    reference that isn't being assigned to, it is treated as an
    error. If you enable warnings, you'll be notified of an
    uninitialized value whenever you treat "undef" as a string
    or a number. Well, usually. Boolean ("don't-care")
    contexts and operators such as "++", "--", "+=", "-=", and
    ".=" are always exempt from such warnings.

    Paul Lalli
    Paul Lalli, Sep 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,223
    Eric J. Roode
    Oct 11, 2003
  2. Liang
    Replies:
    2
    Views:
    751
    Liang
    Aug 11, 2004
  3. Replies:
    1
    Views:
    3,014
    Jens Thoms Toerring
    May 25, 2008
  4. Replies:
    4
    Views:
    159
    Anno Siegel
    Sep 20, 2005
  5. neilsolent
    Replies:
    3
    Views:
    134
    Justin C
    Nov 26, 2009
Loading...

Share This Page