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

Dear Monks,

Why does
my $file_name = "$sorted_files[0]";
Give me a 'use of uninitialised value' error message, but the following does not:
my $file_name = $sorted_files[0];

Replies are listed 'Best First'.
Re: 'use of uninitialised value' error message
by GrandFather (Saint) on Apr 12, 2006 at 10:19 UTC

    Because the former interpolates a variable into a string which involves stringising the variable and that fails if the variable is not defined.

    The latter is simply copying the contents of a variable that happens to be undef into another variable. No interpretation of the contents of the variable required so no error.


    DWIM is Perl's answer to Gödel
Re: 'use of uninitialised value' error message
by graq (Curate) on Apr 12, 2006 at 11:59 UTC

    Because in the first snippet you are making use of the value of $sorted_files[0] and in the second you are not.

    -=( Graq )=-