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

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

am 06.04.2008 08:53:09 von vikimun

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.

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

am 06.04.2008 10:34:40 von Johann Kappacher

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

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

am 06.04.2008 10:41:46 von Johann Kappacher

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

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

am 06.04.2008 18:50:58 von Chris Mattern

On 2008-04-06, 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.

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

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

am 06.04.2008 19:47:24 von Johann Kappacher

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!

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

am 07.04.2008 11:40:07 von Abigail

_
Chris Mattern (syscjm@sumire.gwu.edu) wrote on VCCCXXXII September
MCMXCIII in :
{} On 2008-04-06, 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.
{}
{} 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.


Shorter:

if (length ($var // "")) { .. }



Abigail
--
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'