Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Use of uninitialized value in formline?

by MaxKlokan (Monk)
on Mar 05, 2007 at 13:26 UTC ( #603206=perlquestion: print w/replies, xml ) Need Help??

MaxKlokan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
why does the following code give the warnings below, and why does it do so even if warnings are not turned on?
Incidentally, using no warnings prevents the warnings from appearing.

Code:
#!perl my @a = qw/one two three four five/; $~ = "TEST"; write; format TEST = ---+++--- a: @* @* @* ~~ shift @a, shift @a, shift @a --------- .
Output:
---+++--- a: one two three a: four five --------- Use of uninitialized value in formline at test.pl line 10. Use of uninitialized value in formline at test.pl line 10. Use of uninitialized value in formline at test.pl line 10. Use of uninitialized value in formline at test.pl line 10.

Replies are listed 'Best First'.
Re: Use of uninitialized value in formline?
by lin0 (Curate) on Mar 05, 2007 at 15:00 UTC

    Hi MaxKlokan,

    why does the following code give the warnings below

    From the documentation:

    “If you put two contiguous tilde characters "~~" anywhere into a line, the line will be repeated until all the fields on the line are exhausted, i.e. undefined.”

    In your example, it prints the first line without problem. For the second line, the last field is undefined (shift @a returns undefined). For the third line, all the fields return undefined, so it stops repeating the line.

    I am not sure if this would help, but you could try something like:

    #!/usr/bin/perl use strict; use warnings; my @a = qw/one two three four five six seven /; $~ = "TEST"; print "---+++---\n"; my ($a1, $a2, $a3); while (@a) { $a1 = shift @a || "empty"; $a2 = shift @a || "empty"; $a3 = shift @a || "empty"; write; } print "---------\n"; format TEST = a: @* @* @* $a1, $a2, $a3 .

    Output:

    ---+++--- a: one two three a: four five six a: seven empty empty ---------

    Cheers,

    lin0

    PS: I do not get any warning running your code with Perl 5.8.7

      Alternatively with just a little extra the original code could work:
      #!perl -w my @a = qw/one two three four five/; $~ = "TEST"; write; format TEST = ---+++--- a: @* @* @* ~~ (shift @a||'',shift @a ||'',shift @a||'') --------- .
      Output:
      ---+++--- a: one two three a: four five ---------
        Great! You win :-)
        Concerning the warnings, I figured out that they appear regardless of use warnings just because I am running perl within Eclipse and I have set it to use warnings by default.
        Thanks everyone for the kind help!
Re: Use of uninitialized value in formline?
by ikegami (Pope) on Mar 05, 2007 at 14:14 UTC

    I get no warnings in Perl 5.6.0, 5.6.1, 5.8.0 and 5.8.8 (although the output is different in the first 3).
    What version of Perl are you using?
    Are you sure you didn't use perl -w script.pl?

Re: Use of uninitialized value in formline?
by vrk (Chaplain) on Mar 05, 2007 at 14:50 UTC

    I get warnings if I use -w. Unfortunately, I know little about formatted output, but do try this one:

    my @a = qw/one/; $~ = "TEST"; write; format TEST = ---+++--- a: @* ~~ shift @a --------- .

    Output with Perl 5.8.8:

    Use of uninitialized value in formline at p.pl line 10. ---+++--- a: one ---------

    My guess is that write wants to print one more line than there are data elements for. In your first example, this happens four times, because after the row

    a: one two three

    the array contains only two elements. write tries to shift three, and you get your first "uninitialized value" warning. Then write tries to print one line more, but the array is exhausted of elements, and you get thus three more warnings. In my shorter example, there is only one warning, because each line contains only one shift.

    I have no clue whether this is a bug or a feature. I can remember using formatted output only once in my life, and that was years ago.

    --
    print "Just Another Perl Adept\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://603206]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-10-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (248 votes). Check out past polls.

    Notices?