in reply to Re: Creating 'new' subroutine.
in thread Creating 'new' subroutine.

> Is it just me or isn't that equivalent to: $options = { @_ };

Good point, w/o testing I'd say yes !

Maybe the intention was to check if @_ has even number of elements?

update

Still with testing.

DB<112> use Data::Dump qw/pp/ DB<113> sub tst { pp (scalar @_ ? {@_} : {}); pp {@_};()} DB<114> tst {} {} DB<115> tst a { a => undef } { a => undef } DB<116> tst a,1 { a => 1 } { a => 1 }

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^3: Creating 'new' subroutine.
by tobyink (Canon) on Oct 11, 2013 at 12:04 UTC

    If we're guessing at intentions, then I might wager:

    $options = scalar(@_) == 1 ? $_[0] : { @_ };

    Though that's probably better written as:

    $options = { scalar(@_) == 1 ? %{$_[0]} : @_ };

    ... to deliberately shallow clone in the case of a hashref.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re^3: Creating 'new' subroutine.
by Anonymous Monk on Oct 11, 2013 at 20:35 UTC
    Even if the intention was to write it with a check for evenness, @_ % 2 == 0 ? {@_} : {}, it would be very poor coding practise to just silently discard the options supplied by the user (if odd).
Re^3: Creating 'new' subroutine.
by boftx (Deacon) on Oct 12, 2013 at 00:52 UTC

    My own quick check confirms what LanX (Rolf) just posted. But with respect for checking for an even number of elements, I wouldn't bother.

    One would probably want the fatal error if an odd number was present sense one would most likely be expecting named args coming in. Letting it die is probably the correct course of action.

    The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.