Home > Archive > PERL CGI Beginners > February 2005 > Re: Challenge (for me, at least)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Challenge (for me, at least)
|
|
|
| --- Tyson Sommer <tysons@firstcash.com> wrote:
> Is there any way to squeeze the following into one line? (assume
> using
> CGI.pm and that my $q = new CGI;):
>
> $device = $q->param( "device_name" );
> $device = $ARGV[0] if ! $device;
> $device = "default_device" if ! $device;
my $device = $q->param("device_name") || $ARGV[0] ||
"default_device";
However, be forewarned that any "false" value will trigger that, so in
the unlikely event that 0 (zero) is a valid device name, this is a bug.
When 5.10 finally arrives (and I believe this is available in 5.9),
you'll be able to do this:
my $device = $q->param("device_name") // $ARGV[0] //
"default_device";
That tests whether or not the value is defined and is frequently more
correct.
> Or better yet, how about squeezing this into one line:
>
> $device = $q->param( "device_name" );
> $device = $ARGV[0] if ! $device;
> chomp ( $device = <STDIN> ) if ! $device;
That would be tougher, but I wouldn't squeeze all of that onto one line
because after a bit, readability suffers. Also, if you have a CGI
script, just what are you expecting to read from <STDIN> in that third
line? :)
Cheers,
Ovid
=====
If this message is a response to a question on a mailing list, please send
follow up questions to the list.
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
| |
| Tyson Sommer 2005-02-21, 3:55 pm |
|
> -----Original Message-----
> From: Ovid [mailto:publiustemp-beginnerscgi@yahoo.com]
> Sent: Friday, February 18, 2005 5:35 PM
> To: tysons@firstcash.com; beginners-cgi@perl.org
> Subject: Re: Challenge (for me, at least)
>
> --- Tyson Sommer <tysons@firstcash.com> wrote:
> (assume using
>
> my $device = $q->param("device_name") || $ARGV[0] ||
> "default_device";
>
> However, be forewarned that any "false" value will trigger
> that, so in the unlikely event that 0 (zero) is a valid
> device name, this is a bug.
Definitely not an issue. Device names will never be "0" here. I understand
what you're saying, tho, and will keep this in mind if I ever want to use it
somewhere else. And I'll probably have javascript check the input on the
HTML form before it ever leaves the client.
> When 5.10 finally arrives (and I believe this is available
> in 5.9), you'll be able to do this:
>
> my $device = $q->param("device_name") // $ARGV[0] //
> "default_device";
>
> That tests whether or not the value is defined and is
> frequently more correct.
Sweet!
>
> That would be tougher, but I wouldn't squeeze all of that
> onto one line because after a bit, readability suffers.
> Also, if you have a CGI script, just what are you expecting
> to read from <STDIN> in that third line? :)
Well, I'd like to run the script from a console (myself) as well as via CGI
(others). Possibly more importantly, tho, this is a learning experience for
me. q:^)
Thanks Ovid!
Tyson
P.S., Thanks for the CGI course!
|
|
|
|
|