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

Dear Monks,

Please enlightment me as to the following Perl confusion:

Take a hash construction such as:

my %args = ( 'keyA' => 'valueA', 'keyB' => get_value_b(), 'keyC' => 'valueC' );

Now if get_value_b returns an "undef" (e.x. "return undef;") everything works as expected.

When the get_value_b function returns, nothing (i.e. "return;"), the hash gets thwaked, and ends up constructed as if it was written:

'keyA' => 'valueA',
'keyB' => 'keyC',
'valueC' => undef

So the question is, is this a bug, or a feature?

Replies are listed 'Best First'.
Re: Bug or Feature? Hash construct and non-value returning functions.
by diotalevi (Canon) on Dec 29, 2005 at 21:54 UTC

    It's a bug in your function get_value_b. It is called in list context. Return values in list context return an empty list which is interpolated as nothing which then throws your hash initialization off. If you're going to include function return values in your hash initializations, you'd need to declare scalar context for it so you get an undef value instead.

    my %args = ( 'keyA' => 'valueA', 'keyB' => scalar get_value_b(), 'keyC' => 'valueC' );

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Good catch! I'd give you more ++ if I could. :)