Chovy’s Blog

Conditionally Checking Existence of a Value in Perl

Fri, June 1, 2007 — Category: Perl

Conditionally checking whether or not a variable exists in Perl.

There are many half-baked methods used for properly testing whether or not a variable is defined. Following are a few examples of some good and not-so good methods with explanations.


if (exists $opts{'primary'} || exists $opts{'secondary'}) {
    $some_value = $opts{'primary'} || $opts{'secondary'};
}

The above logic is clean and easy to follow at first glance, although it may not be the most efficient way of executing the appropriate decision making block.

Considering the above is equivalent to the following block, you may notice a somewhat redundant conditional checking that ensues:


if (exists $opts{'primary'} || exists $opts{'secondary'}) {
    if ( exists $opts{'primary'} ) {
        $some_value = $opts{'primary'};
    } else {
        $some_value = $opts{'secondary'};
    }
}

Another added benefit is that if your value is “0″ or empty, it may actually not get assigned if you use the simple one line conditional assignment:


$some_value = $opt{’primary’} || $opt{’secondary’};

The “primary option” would be skipped if it is equal to “0″…which is in-fact a completely valid value, and demonstrates the “falsey-ness” and “truthy-ness” of specific values…more on that later.

Thanks to “Ani-_” in #perl for setting me straight on conditional checking.

So, to some it all up in one bullet-proof conditional block for assignment we write the following, which will also turn out to run faster because there is only one conditional check:


if ( exists $opt{'primary'} ) {
    $some_value = $opt{'primary'};
elsif ( exists $opt{'secondary'} ) {
    $some_value = $opt{'secondary'};
} else {
    return false;
}

  • Post Conditionally Checking Existence of a Value in Perl to del.icio.us
  • Post Conditionally Checking Existence of a Value in Perl to digg
  • Post Conditionally Checking Existence of a Value in Perl to Furl
  • Add Conditionally Checking Existence of a Value in Perl to YahooMyWeb
  • Simpify!
  • Post Conditionally Checking Existence of a Value in Perl to shadows
  • Post Conditionally Checking Existence of a Value in Perl to Spurl
  • Post Conditionally Checking Existence of a Value in Perl to BuddyMarks
  • Submit Conditionally Checking Existence of a Value in Perl to Slashdot

Finding an Intersection Between Arrays in Perl

Thu, May 17, 2007 — Category: Perl

Short recipe for grabbing an intersection between two arrays in Perl.

As explained on perl monks and Pete Kruckenberg’s post…I needed a similar function that returns an intersection between two arrays in Perl.


#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my @array1 = (1, 2, 3);
my @array2 = (2, 3, 4);
my %original = ();
my @isect = ();

map { $original{$_} => 1 } @array1;
@isect = grep { $original{$_} } @array2;

print Dumper(@isect);

#outputs
#$VAR1 = 2;
#$VAR2 = 3;

The above code works with duplicates, meaning that duplicates in @array2 will be added as individual elements in the array @isect.

  • Post Finding an Intersection Between Arrays in Perl to del.icio.us
  • Post Finding an Intersection Between Arrays in Perl to digg
  • Post Finding an Intersection Between Arrays in Perl to Furl
  • Add Finding an Intersection Between Arrays in Perl to YahooMyWeb
  • Simpify!
  • Post Finding an Intersection Between Arrays in Perl to shadows
  • Post Finding an Intersection Between Arrays in Perl to Spurl
  • Post Finding an Intersection Between Arrays in Perl to BuddyMarks
  • Submit Finding an Intersection Between Arrays in Perl to Slashdot

Learn to write “Proper Perl”

Tue, March 13, 2007 — Category: Perl, Open Source, Development

Writing “Proper Perl” can take years to master, and even then you may learn a few more tips…all you have to do is validate against “the critic”…

Perl::Critic is an excellent way to not only validate your Perl source code, but to learn some tips from the Perl monks who have contributed to the Perl::Critic documentation.

I uploaded one of my latest CGI scripts written in Perl to see how good my Perl chops are…turns out I picked up a few tips that I was unsure about in my previous Perl scripts.

A few simple techniques can be used to make your code more standardized in the event that your are not the only person reading it.

The Perl Critic web site has a nice usable web interface for uploading source code and checking its validity — although for obvious reasons if you’re working on enterprise code you should probably download an install the Perl::Critic CPAN module on your own server.

  • Post Learn to write “Proper Perl” to del.icio.us
  • Post Learn to write “Proper Perl” to digg
  • Post Learn to write “Proper Perl” to Furl
  • Add Learn to write “Proper Perl” to YahooMyWeb
  • Simpify!
  • Post Learn to write “Proper Perl” to shadows
  • Post Learn to write “Proper Perl” to Spurl
  • Post Learn to write “Proper Perl” to BuddyMarks
  • Submit Learn to write “Proper Perl” to Slashdot
 
Keyword Advertisers:
SEO Directory SEO Links Free Link Directory Shopping Submission Directory Gardening Tips Political Forum Search Engine Optimization Search Engine Marketing Audio Video Directory SEO Forum Web Development Blog Organic SEO Wiki Web Development Consulting

Learn more about purchasing keyword text link ads on this site.