Click here to get back home

Shortcut for if(defined($var) && $var ne "") ?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Shortcut for if(defined($var) && $var ne "") ? vikimun 04-06-2008
Posted by vikimun on April 6, 2008, 2:53 am
Please log in for more thread options
Is there shorter equivalent of if(defined($var) && $var ne "")
that doesn't fall for the "0" case, and doesn't produce warning with -
w ?

Thanks
V.M.

Posted by Johann Kappacher on April 6, 2008, 4:34 am
Please log in for more thread options
vikimun@gmail.com wrote:
> Is there shorter equivalent of if(defined($var) && $var ne "")
> that doesn't fall for the "0" case, and doesn't produce warning with -
> w ?
>
> Thanks
> V.M.
Hmm,

if (length $var) ....

--jk

Posted by Johann Kappacher on April 6, 2008, 4:41 am
Please log in for more thread options
Sorry,

it will produce warnings if it is not initialized.
If you want to avoid warnings you must use defined().

perl 5.10 introduces the defined-or Operator.
=> This is your solution.

--jk

Posted by Johann Kappacher on April 6, 2008, 1:47 pm
Please log in for more thread options
Johann Kappacher wrote:
> Sorry,
>
> it will produce warnings if it is not initialized.
> If you want to avoid warnings you must use defined().
>
> perl 5.10 introduces the defined-or Operator.
> => This is your solution.
>
> --jk

... but no, you need a defined-and operator!
I give up, this exercise is futile!

Posted by Chris Mattern on April 6, 2008, 12:50 pm
Please log in for more thread options
> Is there shorter equivalent of if(defined($var) && $var ne "")
> that doesn't fall for the "0" case, and doesn't produce warning with -
> w ?
>
> Thanks
> V.M.

I tried this test program:

#!/usr/bin/perl

use warnings;
use strict;

my $var;

if (defined($var) && $var ne "") {
print "$var\n";
}

It didn't produce any warnings. When I put in "my $var = 0;",
it printed "0", just like it should. I don't understand
your question. If by "shorter" you mean getting rid of the
defined() test, then, no, you can't get rid of that. If you
aren't sure if $var is going to be defined, you have to test
for it before trying to use it.

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities

Similar ThreadsPosted
Windows shortcut without dos prompt August 3, 2005, 2:29 pm
short form for (defined $x and defined $y and ... )? March 25, 2006, 12:14 am
Where is EOM defined? August 16, 2005, 1:01 pm
Where are Inline_Stack_Vars, etc. defined? July 30, 2005, 7:36 am
not defined v. eq undef January 25, 2008, 4:19 am
Where was a function defined? September 2, 2006, 6:39 pm
How can I find out where a subroutine is defined? September 29, 2004, 8:48 pm
SysV constants, where are they defined? November 17, 2004, 10:16 am
Question on "defined" function February 22, 2006, 10:38 am
Example for User-defined subroutines for GetOptions please June 24, 2005, 9:51 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap