Soncire
Soncire

Reputation: 309

Perl If statement without parameters just plain variable is the EXP

I just wanted to know what does this code do?

my $string_1 = "foo bar";
my $val = 3;

if($string_1) {  


}

basically what happen if you just use a variable inside an if statement?

thanks

Upvotes: 1

Views: 108

Answers (2)

simbabque
simbabque

Reputation: 54373

It checks if the value of the variable is true. In Perl, everything is true but the following:

  • 0 and the string '0'
  • undef
  • () (the empty list)
  • '' (an empty string)

This is documented in perlsyn. It also works with any other kind of value. You can also put a string, a function call inside the if condition. The behavior is always the same.

Upvotes: 4

user1244533
user1244533

Reputation: 101

this will check variable '$string' has not null value

Upvotes: 0

Related Questions