chargrill has asked for the wisdom of the Perl Monks concerning the following question:
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.
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested ternaries
by GrandFather (Saint) on Feb 15, 2006 at 23:24 UTC | |
by chargrill (Parson) on Feb 16, 2006 at 01:36 UTC | |
by GrandFather (Saint) on Feb 16, 2006 at 01:39 UTC | |
by chargrill (Parson) on Feb 22, 2006 at 17:40 UTC |