Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Annoying 'Use of uninitialized value in concatenation' warning by Perlbotics
in thread Annoying 'Use of uninitialized value in concatenation' warning by alain_desilets

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found