use CGI qw/:standard/; my $q = new CGI; my $choice = param('choice') || 1;

This has nothing to do with your question but you seem slightly confused by the function-based and OO interfaces to CGI.pm. You're creating a CGI object, but then ignoring it to use the function-based interface. You don't need that my $q = new CGI;.

As for your problem, your code is just doing what you are telling it to do. Your code is saying "set $choice to the parameter 'choice', unless that parameter has a false value, in which case set $choice to be 1". 0 has a false value, so if you send a parameter of 0 then you'll end up with 1.

You need to understand the difference between a value being "defined" and a value being "true". Here, you are interested in whether the parameter is defined, not whether it is true.

In the forthcoming Perl 5.10, there is a new "defined-or" operator that does exactly what you want. So you'll be able to write:

my $choice = param('choice') // 1;

But that's not out yet, so you'll have to do something like this:

my $choice = param('choice'); $choice = 1 unless defined $choice;
--

See the Copyright notice on my home node.

"The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg


In reply to Re: param = 0, not NULL by davorg
in thread param = 0, not NULL by Spenser

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.