Note that || does properly propagate list context onto its right operand. The problem is that bless has a "$;$" prototype, so there isn't list context, there's scalar context instead. So calling it as a function results in a blessing into class "0" :)# instead of my $class = shift; my $self = bless {}, ref($class) || $class; # try my $self = bless {}, ref($_[0]) || @_
open also suffers from this (it has a "*;$@" prototype). So if you want a sub to do some processing and then turn around and open a file passing through its parameters to open, you can't do:
Instead you need:sub foo { # some stuff open my $fh, @_; return $fh; }
which IMO is real ugly (TM).sub foo { # some stuff my $fh; if (@_ == 1) { open $fh, $_[0]; } else { open $fh, $_[0], $_[1], @_[2..$#_]; } return $fh; }
What bothers me most is that I can't think of any good reason *why* the optional args should have scalar context for either bless or open. Prototypes of "$@" and "*@" would be just as good in any case I can think of. I can't help but think that someone fell prey to what I call prototypitis; that is, using prototypes to describe what a function expects, rather than using them to control how a function is called.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: multi-arg bless (core==prototyped)
by tye (Sage) on Jul 13, 2004 at 03:24 UTC | |
|
Re: multi-arg bless
by jeffa (Bishop) on Jul 13, 2004 at 18:20 UTC | |
|
Re: multi-arg bless
by rir (Vicar) on Jul 13, 2004 at 17:04 UTC | |
by ysth (Canon) on Jul 13, 2004 at 17:25 UTC |