I'm writing a CGI where I'd like the user to select a layout for a series of thumbnails, and store that selection in a cookie, but let the user override the value stored in the cookie by changing their selection via a link. I thought I'd get away without having several big if/elsif/else blocks (there are a few user-definable parameters) by using the following code:

use CGI qw/:standard/; use CGI::Cookie; #... my %cookies = fetch CGI::Cookie; #... $rows = $cookies{'rows'} ? ( param( 'rows' ) ? param( 'rows' ) : $cookies{'rows'}->value ) : ( param( 'rows' ) and param( 'rows' ) =~ /^\d{1,2}$/ ? param( 'row +s' ) : 5); #... my $rowscookie = new CGI::Cookie( -name => 'rows', -value => $rows, -expires => '+3M', ); #... print header(-cookie=>[$rowscookie,$colscookie]); #...

... which I thought was working fine until I tried it from a browser that didn't already have a cookie, in which I noticed that I get _no_ value in $rows.

I've been scratching my head on this for a little while, and have deduced that I'm having trouble seeing the error of my own ways, and I'm hoping someone else can point a finger at my problem. I've included the other relevant code in case the problem lies outside my nested ternaries.

Note: $colscookie is constructed exactly the same way, and is suffering exactly the same problem.

Update: Not sure if it's obvious from the code, but failing the presense of a cookie value or a link param, I'd like it to default to 5.



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

In reply to 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.