Use of uninitialized value in open

M

Mark

I get "Use of uninitialized value in open" the second time through the
loop. However, if I declare $buff before the loop (and not in the
loop), I don't get the error.

Can someone explain what is happening?

use strict;
use warnings;

$| = 1;
#my $buff;

foreach (1..2) {
print "starting iteration $_\n";
my $buff;
open(my $TMPBUF,'>',\$buff) || die "open, $!\n";
print {$TMPBUF} "$_\n";
close($TMPBUF);

# print "buff=>$buff<\n";
}

Output::
starting iteration 1
starting iteration 2
Use of uninitialized value in open at j.pl line 10.
 
P

Peter J. Holzer

I get "Use of uninitialized value in open" the second time through the
loop. However, if I declare $buff before the loop (and not in the
loop), I don't get the error.

Can someone explain what is happening? [...]
foreach (1..2) {
print "starting iteration $_\n";
my $buff;
open(my $TMPBUF,'>',\$buff) || die "open, $!\n"; [...]
}

Output::
starting iteration 1
starting iteration 2
Use of uninitialized value in open at j.pl line 10.

I get this warning with perl 5.8.8 but not with 5.10.1, so I guess it's
a bug which was fixed in perl 5.10.

As a workaround you can just assign an empty string to $buff:

my $buff = "";

hp
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top