Is there a simpler syntax for: defined $a && $a ne '' 
Author Message
 Is there a simpler syntax for: defined $a && $a ne ''

I frequently need to ensure that a scalar contains "something"; i.e.,
that it is defined and that it contains some non-empty value.  I usually
try something like this:

    $a = 1 unless (defined $a and $a ne '');

Is there a simpler, more concise syntax for the "unless" part of this
expression?  I've tried things like:

    $a = 1 unless length $a;

and

    $a = 1 unless $a =~ /./;

but these don't work unless $a is already defined.  Also, I can't just
test for true or false, because '0' is false, but would be acceptable
under my criterion.

I know this is not exactly a matter of earth-shattering importance,
but it would be nice to find a cleaner (shorter) idiom to express this
test.

Thanks,
Brett

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Brett W. Denner                                    Lockheed Martin TAS

748            
 (817) 935-1144 (voice)                             Fort Worth, TX 76101
 (817) 935-1212 (fax)                               MZ 9333



Mon, 28 Feb 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''


..wants a neater way to achieve..

Quote:
> >     $a = 1 unless (defined $a and $a ne '');
> The Llama book, 1st Ed, p12 (footnote), says undef 'looks like an empty
> string to the eq operator'. Therefore you could use...

> $a = 1 if $a eq '';

Yes, but I _think_ the questioner is trying to avoid provoking a "use of
uninitialized value" warning at that point.  I'm afraid your suggestion
causes that warning too.

Under the warning message itself, the only suggestion seems to be to
initialize all variables before use.  This doesn't always address the
problem, does it, since if $a gets set to the result of some expression,
it could, depending on circumstances, result in $a becoming undefined,
even though it had been initialized before?

No, sorry, I don't know a better answer.  Good job there are some
experts around here, maybe they can help, or tell us definitively there
isn't a better answer...



Thu, 02 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:

> I frequently need to ensure that a scalar contains "something"; i.e.,
> that it is defined and that it contains some non-empty value.  I usually
> try something like this:

>     $a = 1 unless (defined $a and $a ne '');

> Is there a simpler, more concise syntax for the "unless" part of this
> expression?  I've tried things like:

>     $a = 1 unless length $a;

> and

>     $a = 1 unless $a =~ /./;

> but these don't work unless $a is already defined.  Also, I can't just
> test for true or false, because '0' is false, but would be acceptable
> under my criterion.

The Llama book, 1st Ed, p12 (footnote), says undef 'looks like an empty
string to the eq operator'. Therefore you could use...

$a = 1 if $a eq '';

HTH,

Russ

---------------------------------------------------------------------

--[  University of York, UK  ]---[ http://www.york.ac.uk/~rjo100/ ]--
---------------------------------------------------------------------

---------------------------------------------------------------------



Thu, 02 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Russell> The Llama book, 1st Ed, p12 (footnote), says undef 'looks
Russell> like an empty string to the eq operator'. Therefore you could
Russell> use...

Russell> $a = 1 if $a eq '';

Or, to be Way Cooler (as we say on the left coast):

        $a ||= 1;

Russell> HTH,

EIEIO!

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 351 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Fri, 03 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:


> Russell> The Llama book, 1st Ed, p12 (footnote), says undef 'looks
> Russell> like an empty string to the eq operator'. Therefore you could
> Russell> use...

> Russell> $a = 1 if $a eq '';

that would give an 'uninitialized' warning under -w
if $a was not defined.

Quote:
> Or, to be Way Cooler (as we say on the left coast):

>         $a ||= 1;

I think the original poster wanted to accept $a if it was zero,
just set to 1 if it was undefined or ''.

defined($a) && $a ne ''||($a=1);

is very slightly shorter than the original, but I'd stick
with the original version:

$a=1 unless (defined $a and $a ne '');

...it reads much better

regards
andrew



Fri, 03 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:


> Russell> The Llama book, 1st Ed, p12 (footnote), says undef 'looks
> Russell> like an empty string to the eq operator'. Therefore you could
> Russell> use...

> Russell> $a = 1 if $a eq '';

> Or, to be Way Cooler (as we say on the left coast):

>         $a ||= 1;

Ah, but it doesn't work if $a='0'; Part of the question was:

Quote:
>> Also, I can't just
>> test for true or false, because '0' is false, but would be acceptable
>> under my criterion..

Yes, it is Very Cool, though, I didn't realise you could do that :-)

Later,

Russ

---------------------------------------------------------------------

--[  University of York, UK  ]---[ http://www.york.ac.uk/~rjo100/ ]--
---------------------------------------------------------------------

---------------------------------------------------------------------



Fri, 03 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:

>> Russell> $a = 1 if $a eq '';

>that would give an 'uninitialized' warning under -w
>if $a was not defined.

That's only a warning, not an error.

If it annoys you (it clutters your output, hiding really interesting
warnings), you can temporarily disable warnings by clearing $^W:

        #perl -w
        print $xyz;
        {
                local($^W);
                print $xyz;
        }
        print $xyz;

You'll get a warning for the first and the last statement, but not for
the statement inside the block.

HTH,
Bart.



Fri, 03 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:


> > The Llama book, 1st Ed, p12 (footnote), says undef 'looks like an empty
> > string to the eq operator'. Therefore you could use...

> > $a = 1 if $a eq '';

> It works, unless I've started perl with -w, in which case I still get a
> warning for using an undefined value, which I'm trying to avoid.

Just define it somewhere, then: put "$a = '';" somewhere right at the
start of your script.

Randal's suggestion of "$a ||= 1;" would work fine then.

HTH,

Russ

---------------------------------------------------------------------

--[  University of York, UK  ]---[ http://www.york.ac.uk/~rjo100/ ]--
---------------------------------------------------------------------

---------------------------------------------------------------------



Fri, 03 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:


> >> Russell> $a = 1 if $a eq '';

> >that would give an 'uninitialized' warning under -w
> >if $a was not defined.

> That's only a warning, not an error.

as I said, a warning...

Quote:
> If it annoys you (it clutters your output, hiding really interesting
> warnings), you can temporarily disable warnings by clearing $^W:

[snip]

yes one can locally disable warnings...but I'm not sure I'd
consider that, in combination with the above quoted example, to
be an example of a "simpler syntax" than the poster's orignal
statement (but maybe that's just me).

regards
andrew



Sat, 04 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

Quote:

> Russell> $a = 1 if $a eq '';

> Or, to be Way Cooler (as we say on the left coast):

>    $a ||= 1;

Not quite. This one sets $a to 1 if $a == 0.

-- Johan



Sat, 04 Mar 2000 03:00:00 GMT  
 Is there a simpler syntax for: defined $a && $a ne ''

I hope this isn't a duplicate, since it looks like the previous
post didn't get out.

Quote:

> I frequently need to ensure that a scalar contains "something"; i.e.,
> that it is defined and that it contains some non-empty value.  I usually
> try something like this:

>     $a = 1 unless (defined $a and $a ne '');

> Is there a simpler, more concise syntax for the "unless" part of this
> expression?

Have you tried using subroutines to handle it?  I am not a perl expert,
but it looks like all 3 of these perform the same function:

$a = 1 if s1($a);
$a = s2($a,1);
s3(\$a,1);

The last 2 could even bring the '1' inside the subroutine if desired.  I used
the following subroutines:

sub s1($) {
    my $v = shift;
    return (!defined($v) or $v eq "");

Quote:
}

sub s2($$) {

    return ((!defined($v) or $v eq "") ? $a : $v);

Quote:
}

sub s3(\$$) {

    $$vref = (!defined($$vref) or $$vref eq "") ? $a : $$vref;

Quote:
}

Any comments on these ideas?
I am sure people will let me know if I made any mistakes.
--



Sat, 04 Mar 2000 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Perl && SAP's RFC

2. differance between &sub and &'sub

3. perl parsing bug - if &foo and scalar(&bar) doesn't work correctly

4. does ms's nmake understand &&

5. PL41 BUG: pack('c',0) ne sprintf("%c",0)

6. Calling subs using '&' form

7. perl bug: perl -de '&{sub{}}'

8. mySQL &amp; Perl -- Something simple

9. system ('myproc &');

10. emulating the shell '&' in perl

11. '&' operator on strings

12. help- &sub unless $form ne @array

 

 
Powered by phpBB® Forum Software