in reply to Odd (to me) case of wrongly guessing context

Your problem comes from the combination of the my ... with the defined-or operator. It's an operator precedence issue: put (my ...) in brackets. You can also use the low-precedence or operator, but only if you can guarantee that 0 will never be a valid return.

sub context { wantarray ? (qw/foo bar/) : -1 } my ($not_foo) = context() // say "Whoops"; say $not_foo; (my ($foo) = context()) // say "Whoops"; say $foo; # This works if context() can never return 0 my ($also_foo) = context() or say "Whoops"; say $also_foo;

Replies are listed 'Best First'.
Re^2: Odd (to me) case of wrongly guessing context
by choroba (Cardinal) on May 20, 2013 at 22:37 UTC
    Note that you can never trigger the right hand side of
    ( my ($ar) = () ) // die "Undefined array in scalar context";
    In other words, you can safely remove the // croak part.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Good catch, but this works

      DB<106> (my ($ar) = () ) || print "bla" bla DB<108> (my ($ar) = (1) ) || print "bla" => 1 DB<109> (my ($ar) = (0) ) || print "bla" => 1 DB<110> (my ($ar) = (undef) )|| print "bla" => 1 DB<111> (my ($ar) = (undef,undef) )|| print "bla" => 2

      list-assignment in scalar context counts and the count is always defined. (see also goatse-operator for comparison.)

      DB<119> scalar ( my ($ar) = () ) => 0

      so only empty-list is 0 and false.

      But I'm not sure if I'm in favour of such constructs...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      Ah yes, quite right. I missed that in my haste.

      OP, the function you're calling doesn't appear to be standard DBI, so I can't advise you (the OP) on how to handle exceptions, but in general, it will probably be one of the following:

      • The function might die() on error; use eval
      • It may return an empty list, in which case your $mx_registered_process_id will be undef (but undef might also indicate something else, such as a NULL column, so be careful; assign the function return to an intermediary @array if that's the case).
      • It may use a package scalar to hold error values (which is set to undef if there was no error.

      Again, this is all just speculative, since I don't know the semantics of your db_select_row() function.