Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Annoying 'Use of uninitialized value in concatenation' warning

by Perlbotics (Chancellor)
on Oct 25, 2011 at 20:06 UTC ( #933689=note: print w/replies, xml ) Need Help??


in reply to Annoying 'Use of uninitialized value in concatenation' warning

You can silence the warning (if you know what you do) or you can detect and treat the undef-case. However, it is wise to keep the no warnings; scope small. Update: See also johngg's hint below about the do{ ... } trick.

When in doubt, I suggest to treat the undef-case, and let the warning pragma do its job. Often, an undef warning reveals a bug that was otherwise undetected.

use strict; use warnings; sub original_post { my @some_array = (1, 2, undef, 4, 5); for (my $ii=0; $ii < scalar(@some_array); $ii++) { print "Value at position $ii: $some_array[$ii]\n"; } } sub detect_undef_and_set { my @some_array = (1, 2, undef, 4, 5); #alternative-1: fix data structure # @some_array = map { $_ // '(oops! undef!!)' } @some_array; for (my $ii=0; $ii < scalar(@some_array); $ii++) { #alternative-2: fix output print "Value at position $ii: ", $some_array[$ii] // '(oops! undef +!)' , "\n"; } } sub no_warnings { my @some_array = (1, 2, undef, 4, 5); for (my $ii=0; $ii < scalar(@some_array); $ii++) { no warnings 'uninitialized'; # for this lex scope only print "Value at position $ii: $some_array[$ii]\n"; } } print "ORIG:\n"; original_post(); print "CHECK:\n"; detect_undef_and_set(); print "NO WARN:\n"; no_warnings(); __DATA__ RIG: Value at position 0: 1 Value at position 1: 2 Use of uninitialized value $some_array[2] in concatenation (.) or stri +ng at nowarn.pl line 7. Value at position 2: Value at position 3: 4 Value at position 4: 5 CHECK: Value at position 0: 1 Value at position 1: 2 Value at position 2: (oops! undef!) Value at position 3: 4 Value at position 4: 5 NO WARN: Value at position 0: 1 Value at position 1: 2 Value at position 2: Value at position 3: 4 Value at position 4: 5

Replies are listed 'Best First'.
Re^2: Annoying 'Use of uninitialized value in concatenation' warning
by johngg (Abbot) on Oct 25, 2011 at 23:35 UTC

    You can confine the no warnings 'uninitialised'; to the lexical scope but you can make it even tighter if you wish by confining it to the print with the use of a do block.

    for (my $ii=0; $ii < scalar(@some_array); $ii++) { print do { no warnings 'uninitialized'; # for the do block only "Value at position $ii: $some_array[$ii]\n"; }; }

    I hope this is of interest.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://933689]
help
Chatterbox?
[ambrus]: Corion: that's not true. Actually for Christmas and Thanksgiving, a lot of people buy electronics such as cameras as present, then many of them figure out they don't need it,
[ambrus]: and the electronics gets reselled almost new, but it has to be sold at half price because otherwise everyone chooses to buy the new product which has fewer risk of selling damaged products labelled as almost new.
[ambrus]: You can actually get a lot of useful cheap really almost new products that way, with only a little risk of scams.
[ambrus]: That's what some of the "Black Friday" sales are about.
[Corion]: ambrus: Well, usually, these people don't have in their description "mail me at dodgy_reseller # g m a i l | co m" , replace the "#" by "@" :)
[Corion]: Oh, and the "o" in "com" is a zero
choroba orders a camera from Ole Scæmmer
[ambrus]: Corion: ah. that's different. the ones I mean are selling at reputable sites like ebay that usually filters scammers out pretty quickly (as well as filters a lot of legitimate users who then get annoyed that the biggest providers exclude them)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2017-11-21 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (304 votes). Check out past polls.

    Notices?