Extreme Perl

davorg on 2004-04-14T10:41:58

I haven't read it all yet, but this looks interesting.

It's public draft of a book on XP in Perl written by Robert Nagler.


Cool but...

zatoichi on 2004-04-14T15:39:47

I certainly don't agree with replacing "if else" with "? :". You throw your readability out the window, IMO. But I guess that is what makes it "extreme".

Re:Cool but...

runrig on 2004-04-14T16:26:52

Each has it's use in it's place. Use "? :" when you need to return a value in a statement inline. E.g.,
my $x = $test ? $y : $z;
# is better (disclaimer - IMHO) than:
my $x;
if ($test) {
  $x = $y;
} else {
  $x = $z;
}
All of the examples of "? :" in that document (that I can see) are similar to this, so they're ok. Using "? :" in a void context is arguably not so good.

Re:Cool but...

zatoichi on 2004-04-14T16:57:37

my $x = $test ? $y : $z;

I can understand doing the above becuase visually you can isolate that line and grok it quickly. When you use it in a larger "else if" context you loose that visual recognition.

whitespace

mary.poppins on 2004-04-14T18:03:50

How about:

  my $foo =   ( $bar =~ /pattern/ )
            ? ( some expression )
            : ( some other expression );

Re:whitespace

rob_au on 2004-04-20T12:57:02

I do something very similar to this in my code on a regular basis - What I normally do is shift the ternary operators back onto the leading line so that it is immediately apparent that the line has not ended. For example:
my $foo = ( $bar =~ /pattern/ ) ?
          ( some expression ) :
          ( some other expression );
This of course is more of a question of style than anything else, and I know that I am somewhat obtuse and anal at times with regard to my layout style.

Re:Cool but...

pudge on 2004-04-20T21:58:49

Like this?
    my $mode = (ref $hash->{CALLBACK} eq 'CODE'
        ? kAEQueueReply
        : (exists $hash->{REPLY}    # check event setting
            ? $hash->{REPLY}
            : exists $self->{REPLY}    # check global setting
                ? $self->{REPLY}
                : 1        # default to wait
        )
            ? kAEWaitReply
            : kAENoReply)
 
        | (exists $hash->{MODE}
            ? $hash->{MODE}
            : exists $self->{MODE}
                ? $self->{MODE}
                : (kAECanInteract | kAECanSwitchLayer));
Sorry. :-)

Re:Cool but...

bart on 2004-04-14T19:59:06

Then try this. (;-))
my $x = do {
    if ($test) {
        $y;
    } else {
        $z;
    }
};