[Israel.pm] A Perl 5 Gotcha

Gabor Szabo szabgab at gmail.com
Thu Mar 5 22:35:10 PST 2009


This is a nice one.
I have several of these listed as test cases that I'd like to catch,
even if partially.

Do you have any idea how to spot such issues or even how to recognize this
one specifically using PPI or B::Deparse or some other such tool?

Gabor

On Thu, Mar 5, 2009 at 11:12 PM, Shlomi Fish <shlomif at iglu.org.il> wrote:
> The following is a simplified version of a program with a bug that bit me
> today:
>
> {{{{{{{{{{{
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print_filename();
>
> my $filename = "hello.txt";
>
> sub print_filename
> {
>    print $filename, "\n";
> }
> }}}}}}}}}}}
>
> Can you see what the problem is? See below for the spoiler.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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().
>
> Regards,
>
>        Shlomi Fish



More information about the Perl mailing list