develooper Front page | perl.perl5.porters | Postings from February 2009

RFC: autodie behaviour in list context

Thread Next
From:
Paul Fenwick
Date:
February 17, 2009 12:55
Subject:
RFC: autodie behaviour in list context
Message ID:
499B2436.2070806@perltraining.com.au
G'day p5p,

The question of what autodie should do in list context came up the other day
on PerlMonks[1].  Put simply, it's unclear under which circumstances autodie
should throw an exception with the following code:

	use Some::Module qw(foo);
	use autodie qw(foo);

	my @results = foo();

The obvious options are:

	a) foo() returns an empty list.
	b) foo() returns a list of a single undef.
	c) foo() returns a list of a single false value.
	d) Either (a) or (b)
	e) All of the above.

Currently, autodie implements behaviour (d), throwing an exception if the
empty list is returned, or if a list consisting of a single undef is
returned.  The first is to catch failures used by a simple 'return;', and
the second to catch the common (context-insensitive) 'return undef' that is
commonly seen in older code.

The question asked on PerlMonks is should autodie *also* throw exceptions in
list context for (c), where a list of a single false value is returned.
This allows autodie to throw exceptions for context-insensitive subroutines
that like to return a false value other than undef to indicate failure.  In
particular, it means the following code will correctly throw exceptions when
copying a file fails:

	use File::Copy;
	use autodie qw(copy);
	use 5.010;

	say copy("/tmp/foo", "/tmp/bar");

The copy() subroutine from File::Copy always returns 0 to indicate failure.
 This means that autodie correctly throws an exception in scalar context
(where it does a fairly simple 'copy() or die...') but merely returns (0) in
list context, since it could conceivably be valid data, and autodie doesn't
know any better.

This is only an issue for user-defined subroutines.  For built-ins, autodie
has a big table of hints and special-behaviours, and always does the right
thing.

I agree the above example is a little odd, but it clearly bit one person
enough to have them write it up on PerlMonks.  There are also some
situations, such as the last statement in a subroutine (which depends upon
calling context), where this could also bite.

So, I'm seeking opinions.  Should autodie keep its current behaviour, or
should it throw an exception for any list consisting of a single false value?

Many thanks for all your input,

	Paul

[1] http://perlmonks.org/?node_id=744246

-- 
Paul Fenwick <pjf@perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About