Perl Shorthand If Clause

in Perl

Perl has a ton of short hand features. One that a lot of non-beginners use, and that I forget frequently, is the perl short hand if clause. This tutorial will show you how to use the shorthand if clause in perl:

Perl “IF” Clause, Normally…

Typically if you want to do an if statement in perl, you would do something like this:

my $data;

  $data = $form->{somefield};
  $data = 1;

print $data;

Perl Shorthand “IF” Clause

But, if you wanted to do the shorthand version of the if statement, here is how you would code it:

my $data = $form->{somefield} ? $form->{somefield} : 1;
print $data;

Another Example of Using the Shorthand Perl IF Clause:

print $form->{somefield} ? $form->{somefield} : 1;

Both examples will either use the data that is in the $hash{value} container, or it will use the number 1 if the hash{value} is empty.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: