in reply to Re^3: mystery function & Odd number of elements in anonymous hash
in thread mystery function & Odd number of elements in anonymous hash

Many functions return the empty list or undef as some sort of error condition. Are you sure @values ought to be empty?

The function in question is an accessor to a hash of sorts. It may return no values (=key does not exist) or multiple values.

And how did adding a wrapper to force context solve the issue?

return scalar func(@_); returns $values[0] which is undef in the case of no values which is fine and doesn't maim my hashes.

Replies are listed 'Best First'.
Re^5: mystery function & Odd number of elements in anonymous hash
by JavaFan (Canon) on Jul 17, 2011 at 22:17 UTC
    Why use a wrapper function when Perl already has one?
    my $hshref = { foo => scalar(func()), bar => "baz" };
    Or, in this case:
    my $hshref = { foo => func()[0], bar => "baz" };
Re^5: mystery function & Odd number of elements in anonymous hash
by CountZero (Bishop) on Jul 18, 2011 at 16:24 UTC
    I am not too certain about your "wrapper" function.

    It gives you an undef in case of a missing result, but it will only show the "first" element of the hash, array or list and you will loose the possibility to recognize multiple results.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I am not too certain about your "wrapper" function.

      I'm happy with it. The vast majority of cases return zero or one results. Multiple results are rare, and I know when to expect them and handle them separately. I can always call the original function when such things are needed.

      I still think that it makes sense for the fat comma to effect scalar context, though.

        I still think that it makes sense for the fat comma to effect scalar context, though.

        It wouldn't be a comma operator if it did. The comma operator builds lists.