develooper Front page | perl.beginners | Postings from September 2009

Ternary operator

Thread Next
From:
Steve Bertrand
Date:
September 10, 2009 21:21
Subject:
Ternary operator
Message ID:
4AA9D059.5070803@ibctech.ca
[ forgive me if it was sent twice. My first should be bounced as bad
sender addr ].

Besides consistently forgetting how to properly spell "ternary", I
can't, for some reason, embed it's use into my brain no matter how much
I read.

Perhaps if someone could show me the way against a personal code snip, I
may finally "get it". What would this look like:

    my $gst    = $self->query->param( "gst${item_num}" );
    my $pst    = $self->query->param( "pst${item_num}" );

    if ( $gst eq 'Yes' ) {
        $gst = $self->tax_rate( 'gst' );
    }
    else {
        $gst = 0;
    }

    if ( $pst eq 'Yes' ) {
        $pst = $self->tax_rate( 'pst' );
    }
    else {
        $pst = 0;
    }

...in some cases I get it ( when reading code ). Then, shortly after
when I try it, my code breaks.

I'm thinking that I'm running into a precedence issue, but normally I
don't have so much trouble remembering a seemingly simple idiom.

Recently, I read this:

my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ;

Disregarding ( but acknowledging ) it's accompanying comment, is it fair
to rephrase it as such ( for my own understanding )?:

- if $_[0] is a hashref, shift @_ , and assign it to $args
- otherwise, assign an empty hashref ( anonymous ) to $args

Steve

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About