Re: bad code to good golf

"A. Pagaltzis" <[email protected]> Sun, 9 Dec 2007 22:00:10 +0100
Newsgroups gmane.comp.lang.perl.fun
Message-ID <20071209210010.GS30766@klangraum>
* Uri Guttman <[email protected]> [2007-12-09 19:25]:
> * Michael G Schwern <[email protected]> writes:
>> sub new {
>>   my $class = shift;
>>   return bless( {@_}, $class );
>> }
> 
> my clean version is:
> 
> sub new {
> 	my ( $class, %self ) = @_ ;
> 	return bless \%self, $class ;
> }
> 
> i don't like using shift for args if i can help it.

Personally I *always* use `shift` for the invocant, but
assignment from `@_` for all other parameters in all but a few
rare circumstances. So methods in my code always read something
like this:

    sub frobnitz {
        my $self = shift;
        my ( $foo, $bar, $baz ) = @_;
        # ...
    }

It’s a nod to the fact that the invocant is not really in the
same class (no pun intended) as the other parameters. Since
`$self` is thus removed from `@_`, and is the *only* thing
removed from it, that also makes it natural to write delegative
code:

        # ...
        $self->get_veeblefitzer()->frobnitz( @_ );
        # ...

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>