Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CGI parameter of 0 behaving strangely

by Ming (Acolyte)
on Jun 12, 2003 at 15:41 UTC ( [id://265392]=perlquestion: print w/replies, xml ) Need Help??

Ming has asked for the wisdom of the Perl Monks concerning the following question:

This code doesn't delete the first line when passed a parameter of delete=0. It works for all other values. How come?
use CGI qw(:standard); if(param('delete')) { $lineNum = param('delete'); open(FILE, "$filename"); @lines = <FILE>; close(FILE); splice(@lines, $lineNum, 1); open(FILE, ">$filename"); print FILE (@lines); close(FILE); }

Replies are listed 'Best First'.
Re: CGI parameter of 0 behaving strangely
by hardburn (Abbot) on Jun 12, 2003 at 15:48 UTC

    Because when delete=0, then param('delete') returns 0. When evaluated in boolean context (which is what if does), 0 is false and all other values are true.

    I think what you want is if(defined param('delete')) { . . . }. defined() returns true if the value given is actually defined, but doesn't care what it was defined as.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I see... thank you.
Re: CGI parameter of 0 behaving strangely
by edan (Curate) on Jun 12, 2003 at 15:48 UTC

    Because in perl, zero ('0') is false, so the 'if' test fails.

    --
    3dan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://265392]
Approved by edan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found