Quantcast
Channel: In Perl, how can I concisely check if a $variable is defined and contains a non zero length string? - Stack Overflow
Browsing all 10 articles
Browse latest View live

Answer by daotoad for In Perl, how can I concisely check if a $variable is...

The excellent library Type::Tiny provides an framework with which to build type-checking into your Perl code. What I show here is only the thinnest tip of the iceberg and is using Type::Tiny in the...

View Article


Answer by Joseph for In Perl, how can I concisely check if a $variable is...

if ($name ){ #since undef and '' both evaluate to false #this should work only when string is defined and non-empty... #unless you're expecting someting like $name="0" which is false. #notice though...

View Article

Answer by Gaurav for In Perl, how can I concisely check if a $variable is...

In cases where I don't care whether the variable is undef or equal to '', I usually summarize it as:$name = "" unless defined $name;if($name ne '') { # do something with $name}

View Article

Answer by brian d foy for In Perl, how can I concisely check if a $variable...

You often see the check for definedness so you don't have to deal with the warning for using an undef value (and in Perl 5.10 it tells you the offending variable): Use of uninitialized value $name in...

View Article

Answer by Sinan Ünür for In Perl, how can I concisely check if a $variable is...

First, since length always returns a non-negative number,if ( length $name )and if ( length $name > 0 )are equivalent. If you are OK with replacing an undefined value with an empty string, you can...

View Article


Answer by Dave Sherohman for In Perl, how can I concisely check if a...

How aboutif (length ($name || '')) { # do something with $name}This isn't quite equivalent to your original version, as it will also return false if $name is the numeric value 0 or the string '0', but...

View Article

Answer by daotoad for In Perl, how can I concisely check if a $variable is...

It isn't always possible to do repetitive things in a simple and elegant way.Just do what you always do when you have common code that gets replicated across many projects:Search CPAN, someone may have...

View Article

Answer by Adam Bellaire for In Perl, how can I concisely check if a $variable...

As mobrule indicates, you could use the following instead for a small savings:if (defined $name && $name ne '') { # do something with $name}You could ditch the defined check and get something...

View Article


Answer by mob for In Perl, how can I concisely check if a $variable is...

You could say $name ne ""instead of length $name > 0

View Article


In Perl, how can I concisely check if a $variable is defined and contains a...

I currently use the following Perl to check if a variable is defined and contains text. I have to check defined first to avoid an 'uninitialized value' warning:if (defined $name && length $name...

View Article
Browsing all 10 articles
Browse latest View live