[Israel.pm] A Perl 5 Gotcha

Shlomi Fish shlomif at iglu.org.il
Thu Mar 5 13:12:52 PST 2009

The following is a simplified version of a program with a bug that bit me 


use strict;
use warnings;


my $filename = "hello.txt";

sub print_filename
    print $filename, "\n";

Can you see what the problem is? See below for the spoiler.


The problem passes compilation. However, $filename, while it is declared in 
the scope of print_filename() is not assigned during the call to it, because 
the flow of the program did not reach the assignment yet. So it is declared, 
and valid, but not assigned yet and still undef().


	Shlomi Fish

Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://xrl.us/bjn7i

<mauke>    I'm not interested in what you're doing; what are you trying to 
<PerlJam>  mauke: I'm trying to achieve world peace and this regex is
           the last thing standing in my way! ;)

More information about the Perl mailing list