Continually irritated at the ugliness of the nested ternary once placed back into my code:

$scheme = $cookies{'skin'} ? ( ( param( 'scheme' ) and param( 'scheme' ) =~ /^\w{1,6}$/ ) ? param( 'scheme' ) : $cookies{'skin'}->value ) : ( ( param( 'scheme' ) and param( 'scheme' ) =~ /^\w{1,6}$/ ) ? param( 'scheme' ) : 'default' );

Along with an offline suggestion from liverpole of an operator (||=) I had forgotten yields the following test code:

use strict; use warnings; sub ttw { my( $cookie, $param ) = @_; my $str = '(' . (defined $cookie ? $cookie : 'undef'); $str .= ',' . (defined $param ? $param : 'undef') . "):\t"; $str .= $cookie ||= $param && $param =~ /^\w{1,6}$/ ? $param : 'defa +ult'; $str .= "\n"; return $str; } sub ttd { my( $cookie, $param ) = @_; my $str = '(' . (defined $cookie ? $cookie : 'undef'); $str .= ',' . (defined $param ? $param : 'undef') . "):\t"; $str .= $cookie ||= $param && $param =~ /^\d{1,2}$/ ? $param : 5; $str .= "\n"; return $str; } print ttw( 'yellow', 'blue' ); print ttw( 'green', undef ); print ttw( undef, 'teal' ); print ttw( undef, 'chartreuse' ); print ttw( undef, undef ); print ttd( undef, undef ); print ttd( 4, undef); print ttd( 444, undef); print ttd( undef, 3); print ttd( undef, 333); print ttd( 6, 7);

... which prints:

(yellow,blue): yellow (green,undef): green (undef,teal): teal (undef,chartreuse): default (undef,undef): default (undef,undef): 5 (4,undef): 4 (444,undef): 444 (undef,3): 3 (undef,333): 5 (6,7): 6

And finally the finished code re-worked into my original CGI looks much more succint:

$scheme = $cookies{'skin'} ||= param( 'scheme' ) && param( 'scheme' ) =~ /^\w{1,6$/ ? param( 'scheme' ) : 'default';

... but doesn't work. So I try:

$scheme = $cookies{'skin'}->value ||= param( 'scheme' ) && param( 'scheme' ) =~ /^\w{1,6$/ ? param( 'scheme' ) : 'default';

... which throws an error, Can't modify non-lvalue subroutine call at /path/to/viewer.cgi line 74. And now I can't imagine a way to re-work the ||= operator into my code. Which leaves me with my original, working, ugly version.

I guess my point is that it's fine to simplify code for the purposes of testing, but be wary of shortcuts (golfing) taken to eliminate what might appear to be unnecessary syntax in the test version.



--chargrill
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );

In reply to Re^3: Nested ternaries by chargrill
in thread Nested ternaries by chargrill

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.