Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl Golf 101

by liverpole (Monsignor)
on May 26, 2006 at 15:01 UTC ( #551881=note: print w/replies, xml ) Need Help??


in reply to Perl Golf 101

A very nice guide!  chargrill++.

One note, don't forget that the return of a subroutine is the last statement of the subroutine.  So instead of:

sub o{ my ($g,$w) = @_; my $c = 0; map{ map{ /\+/ && $c++; /\-/ && $c--} @$_ } @$g; return [$c,$w] }
You can just do:
sub o{ my ($g,$w) = @_; my $c = 0; map{ map{ /\+/ && $c++; /\-/ && $c--} @$_ } @$g; [$c,$w] }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: Perl Golf 101
by chargrill (Parson) on May 26, 2006 at 16:00 UTC

    You know, the funny thing is that I know that, and that was one of the first changes I made in going from my full, fairly properly coded version to the golfed version. However, I specifically recall that there was at least one of my subs that refused to function correctly after the change. Was it from removing the explicit return, or some other change I made? I don't know, but I do know that restoring the return (and probably some other subtle changes) restored the functionality, so from that point on, dropping the explicit return was left out of my golf bag.

    Update: Ok, it must've been some other subtle change that borked my golfed version, because without any other change, I removed all return's and it worked exactly as designed.

    Then again, I don't recall making the claim of presenting anything beyond perl golf 101 :)



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

      actually a few months ago I remember a discussion on p5p on return. with no explicit return and with control structures it's actually kind of a lottery to guess the actual *return*

      the sad thing was that it was no easy to fix... and if I recall correctly PBP says always use return ...orthogonal to golf

        ...orthogonal to golf

        Indeed - I dare anyone to run perlcritic* against any golfed code :-) (Which is why I posted the big notice about not letting any of these "techniques" make it into production code ;)

        I've gotten in the habit of running perlcritic against pretty much all of my production level code these days, and taking the output with a grain of salt. My .perlcriticrc does contain quite a few things from PBP that I don't particularly agree with...

        * For those unfamiliar with it, perlcritic is a commandline utility that (by default) critiques your code against a set of rules as outlined by Perl Best Practices.



        --chargrill
        $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://551881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-12-09 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?