RSS
 

Archive for the ‘Perl’ Category

Learn to write “Proper Perl”

24 Aug

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.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Retweet
 
Comments Off

Posted in Development, Open Source, Perl

 

Perl IDE – Eclipse May Do It

14 Aug

While trying to figure out an accurate way of counting the number of lines in the various tools my organization uses, I came across Eclipse Integegrated Development Environment. While I’m an Eclipse n00b, I’m starting to see that it’s what I’ve been looking for, but didn’t know what to call it (IDE).

Obviously, it’s open source (GPL), I wouldn’t even mention it if it weren’t. I also came across an article by Matt Enzer Perl Needs Better Tools about Perl IDE’s (specifically EPIC) to help curb the rate at which people are moving away from Perl in the med-large organization. Mainly, it’s lack of a good IDE (I know, VIM rocks, but this is pretty easy once you know how).

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Retweet
 
Comments Off

Posted in Perl

 

Empty is not a non-digit character in Perl

04 Aug

empty is not a non-digit character “\D”
empty is not a digit character either “\d”

The wrong way to do it:

my $f;
$check = ($f =~ m/\D/ ) ? 'non-digit character' : 'assume digit';

The correct way to do it:

my $f;
$check = ($f =~ m/\d/ ) ? 'digit character' : 'assume non-digit';

References:

$ perldoc perlretut
$ perldoc perlre
VN:F [1.9.3_1094]
Rating: 7.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)
Retweet
 
Comments Off

Posted in Perl

 

Conditionally Checking Existence of a Value in Perl

25 Jul

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;
}
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Retweet
 
Comments Off

Posted in Perl

 

Finding an Intersection Between Arrays in Perl

15 Jul

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.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Retweet
 
3 Comments

Posted in Perl