FAQ
Hi all.

I'm after people's thoughts on Perl's warnings about 'Use of
uninitialized value' in various situations ( string comparisons,
subroutine entries, etc ).
I understand what the warning is for.
In my code, it's perfectly OK for my variables to be uninitialised, and
is beyond my control anyway - the data is coming from a database, and an
undef value is perfectly legal ( NULL fields ).

I can make Perl stop complaining about it by placing if() statements
around everything, eg:

if ($value) {
if ($value eq "some_value_to_compare_to") {
# stuff
} else {
# other stuff
}
} else {
# same as other stuff above
}

But this makes things look overly complicated, increases the size of my
code, and I assume slows things down because I'm doing an extra
comparison that I honestly don't need to be doing.

Is there any way to prevent 'Use of uninitialized value' warnings
without doing an extra test as above ( and still keeping warnings turned
on )?

Thanks :)

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak@nusconsulting.com.au
website: http://www.nusconsulting.com.au

Search Discussions

  • Wagner, David --- Senior Programmer Analyst --- WGO at Mar 18, 2005 at 12:43 am

    Daniel Kasak wrote:
    Hi all.

    I'm after people's thoughts on Perl's warnings about 'Use of
    uninitialized value' in various situations ( string comparisons,
    subroutine entries, etc ).
    I understand what the warning is for.
    In my code, it's perfectly OK for my variables to be uninitialised,
    and is beyond my control anyway - the data is coming from a database,
    and an undef value is perfectly legal ( NULL fields ).

    I can make Perl stop complaining about it by placing if() statements
    around everything, eg:
    if you are using :
    use warnings;

    Then you can use 'no warnings;' just like you would use 'use warnings' to turn off. Then you can use 'use warnings' to get it goint again. The better point might be to surround what you want within {}'s and use your 'no warnings' and once you hit the ending }, then will return to using warnings again.

    Wags ;)
    if ($value) {
    if ($value eq "some_value_to_compare_to") {
    # stuff
    } else {
    # other stuff
    }
    } else {
    # same as other stuff above
    }

    But this makes things look overly complicated, increases the size of
    my code, and I assume slows things down because I'm doing an extra
    comparison that I honestly don't need to be doing.

    Is there any way to prevent 'Use of uninitialized value' warnings
    without doing an extra test as above ( and still keeping warnings
    turned on )?

    Thanks :)

    --
    Daniel Kasak
    IT Developer
    NUS Consulting Group
    Level 5, 77 Pacific Highway
    North Sydney, NSW, Australia 2060
    T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
    email: dkasak@nusconsulting.com.au
    website: http://www.nusconsulting.com.au


    *******************************************************
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    *******************************************************
  • Randy W. Sims at Mar 18, 2005 at 1:02 am

    Wagner, David --- Senior Programmer Analyst --- WGO wrote:
    Daniel Kasak wrote:
    Hi all.

    I'm after people's thoughts on Perl's warnings about 'Use of
    uninitialized value' in various situations ( string comparisons,
    subroutine entries, etc ).
    I understand what the warning is for.
    In my code, it's perfectly OK for my variables to be uninitialised,
    and is beyond my control anyway - the data is coming from a database,
    and an undef value is perfectly legal ( NULL fields ).

    I can make Perl stop complaining about it by placing if() statements
    around everything, eg:
    if you are using :
    use warnings;

    Then you can use 'no warnings;' just like you would use 'use warnings' to turn off. Then you can use 'use warnings' to get it goint again. The better point might be to surround what you want within {}'s and use your 'no warnings' and once you hit the ending }, then will return to using warnings again.
    You can be a little more specific with:

    no warnings qw( uninitialized ); # see `perldoc perllexwarn`

    You could also use a default value;

    my $value = fetch_value() || '';

    or what most seem to do is combine the tests:

    if ( defined( $value ) && $value eq 'some_value' ) { ... }
  • Lawrence Statton at Mar 18, 2005 at 1:08 am

    Hi all.
    if ($value) {
    if ($value eq "some_value_to_compare_to") {
    # stuff
    } else {
    # other stuff
    }
    } else {
    # same as other stuff above
    }

    But this makes things look overly complicated, increases the size of my
    code, and I assume slows things down because I'm doing an extra
    comparison that I honestly don't need to be doing.
    It is a terrible thing, not because it's slower: On a modern machine
    (especially now that speculative execution is a pretty much Solved
    Problem) a single branch not taken adds at WORST a nanosecond to
    your program. Even on a vintage 1988 Sun3 a single branch-not-taken
    is going to add maybe 50 nanoseconds.

    If there were one thing I could beat into the brains of all the
    beginners on this list:

    Do not write code that you think will be
    fast. Write code that you think will work.

    The reason it is a terrible thing is because you must have two
    copies of "other stuff", and the odds of those two copies remaining
    identical grows slimmer each day.
    Is there any way to prevent 'Use of uninitialized value' warnings
    without doing an extra test as above ( and still keeping warnings turned
    on )?
    The idiom I use every single day:

    #!/usr/bin/perl

    use strict;
    use warnings;
    use diagnostics;


    my $value;

    if ($value && $value eq 'spring' ) {
    print "Flowers are in bloom\n";
    } else {
    print "No flowers here!\n";
    }

    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
    Computer software consists of only two components: ones and
    zeros, in roughly equal proportions. All that is required is to
    sort them into the correct order.
  • Charles K. Clarkson at Mar 18, 2005 at 2:00 am
    lawrence@cluon.com wrote:

    : The idiom I use every single day:
    :
    : #!/usr/bin/perl
    :
    : use strict;
    : use warnings;
    : use diagnostics;

    My idiom, which is used a lot for testing code on
    various beginner lists, is this. I never did care for the
    verbose output of diagnostics.pm.

    #!/usr/bin/perl

    use strict;
    use warnings;

    #use Data::Dumper 'Dumper';
    #use CGI;

    __END__


    I also have a few editor macros on hand like this one
    for file opening.

    CTRL+ALT+F:

    my $file = '';
    open FH, $file or die qq(Cannot open "$file": $!);

    close FH;


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 18, '05 at 12:24a
activeMar 18, '05 at 2:00a
posts5
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase