Shortcut for if(defined($var) && $var ne "") ?
am 06.04.2008 08:53:09 von vikimunIs 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.
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.
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
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
On 2008-04-06, vikimun@gmail.com
> 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
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!
_
Chris Mattern (syscjm@sumire.gwu.edu) wrote on VCCCXXXII September
MCMXCIII in
{} On 2008-04-06, vikimun@gmail.com
{} > 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";'