Wednesday, May 22, 2013

Using the Perl grep() function


Using the Perl grep() function

Introduction

The grep function is used to filter lists. The basic syntax is
    @out = grep { CODE } @in;
where CODE is some Perl code. For each element of @inCODE is executed with $_ set to the element. If the code returns a true value, the element is placed in the output list; otherwise it is discarded.

Example 1

Say you have a list of strings, and want to keep only those with less than 8 characters.
    #!/usr/bin/perl
    use strict;
    use warnings;

    my @strs = qw(potatoes lemons apricots apples bananas ostriches flamingoes);
    my @short_strs = grep { length $_ < 8 } @strs;
    for my $str (@short_strs) {
        print "$str\n";
    }
This produces the output:
    lemons
    apples
    bananas

Example 2

What if you have a list of files, and want only those ending in .html? We can use grep together with a regular expression:
    #!/usr/bin/perl
    use strict;
    use warnings;

    my @files = qw(index.html test.html test.txt Makefile home.html help.doc README.txt);
    my @html_files = grep { /\.html$/ } @files;
    for my $file (@html_files) {
        print "$file\n";
    }
This produces the output:
    index.html
    test.html
    home.html

See also

    perldoc -f grep

Calculating the average and standard deviation in perl


Calculating the average and standard deviation in perl

The following two subroutines can be used as a drop in replacement for Math::NumberCruncher. This is useful when trying to limit the number of dependencies of your perl script.
 
sub average{
        my($data) = @_;
        if (not @$data) {
                die("Empty array\n");
        }
        my $total = 0;
        foreach (@$data) {
                $total += $_;
        }
        my $average = $total / @$data;
        return $average;
}
sub stdev{
        my($data) = @_;
        if(@$data == 1){
                return 0;
        }
        my $average = &average($data);
        my $sqtotal = 0;
        foreach(@$data) {
                $sqtotal += ($average-$_) ** 2;
        }
        my $std = ($sqtotal / (@$data-1)) ** 0.5;
        return $std;
}
Usage:
$ave = &average(\@array);
#$ave = Math::NumberCruncher::Average(\@array); //OLD WAY
$std = &stdev(\@array);
#$std = Math::NumberCruncher::StandardDeviation(\@array); //OLD WAY

Tuesday, May 21, 2013

HOW TO INSTALL PERL MODULES ON MAC OS X IN 4 EASY STEPS


HOW TO INSTALL PERL MODULES ON MAC OS X IN 4 EASY STEPS

Today at work, I learned how to install Perl modules using CPAN. It’s a lot easier than I thought.
You see, for the past couple of years, I’ve been a bit frustrated because OS X does not come with a whole lot of Perl modules pre-installed, and for all I googled, I couldn’t find an “idiot’s” guide for moderately-savvy-but-not-expert users like myself to install modules and dependencies on demand.
The only instructions I could find point to Fink, which basically installs modules in a path that isn’t included in the Perl @INC variable, meaning you have to manually specify the full path to the modules in every script — which is not a lot of fun if you’re developing on OS X and deploying on Red Hat, for instance.
Moreover, Fink doesn’t seem to make every module available, and it’s not very easy to determine which Fink package you need to install if you need a particular module.
So, with a script that called on several apparently unavailable modules, and a deadline looming, I finally decided to suck it up and figure out how to use CPAN to install them:

1) Make sure you have the Apple Developer Tools (XCode) installed.

These are on one of your install discs, or available as a huge but free download from the Apple Developer Connection [free registration required] or the Mac App Store. I thought I had them, but apparently when we upgraded that computer to Tiger, they went missing.
If you don’t have this stuff installed, your installation will fail with errors about unavailable commands.

1.5) Install Command Line Tools (Recent XCode versions only)

(Thank you to Tom Marchioro for informing me about this step.)
Older versions of XCode installed the command line tools (which are required to properly install CPAN modules) by default, but apparently newer ones do not. To check whether you have the command line tools already installed, run the following from the Terminal:
$ which make
This command checks the system for the “make” tool. If it spits out something like /usr/bin/make you’re golden and can skip ahead to Step 2. If you just get a new prompt and no output, you’ll need to install the tools:
  1. Launch XCode and bring up the Preferences panel.
  2. Click on the Downloads tab
  3. Click to install the Command Line Tools
If you like, you can run which make again to confirm that everything’s installed correctly.

2) Configure CPAN.

$ sudo perl -MCPAN -e shell
perl> o conf init
This will prompt you for some settings. You can accept the defaults for almost everything (just hit “return”). The two things you must fill in are the path to make (which should be /usr/bin/make or the value returned when you run which make from the command line) and your choice of CPAN mirrors (which you actually choose don’t really matter, but it won’t let you finish until you select at least one). If you use a proxy or a very restrictive firewall, you may have to configure those settings as well.
If you skip Step 2, you may get errors about make being unavailable.

3) Upgrade CPAN

$ sudo perl -MCPAN -e 'install Bundle::CPAN'
Don’t forget the sudo, or it’ll fail with permissions errors, probably when doing something relatively unimportant like installing man files.
This will spend a long time downloading, testing, and compiling various files and dependencies. Bear with it. It will prompt you a few times about dependencies. You probably want to enter “yes”. I agreed to everything it asked me, and everything turned out fine. YMMV of course. If everything installs properly, it’ll give you an “OK” at the end.

4) Install your modules. For each module….

$ sudo perl -MCPAN -e 'install Bundle::Name'
or
$ sudo perl -MCPAN -e 'install Module::Name'
This will install the module and its dependencies. Nice, eh? Again, don’t forget the sudo.
The first time you run this after upgrading CPAN, it may prompt you to configure again (see Step 2). If you accept its offer to try to configure itself automatically, it may just run through everything without a problem.
There are a couple of potential pitfalls with specific modules (such as theLWP::UserAgent / HEAD issue), but most have workarounds, and I haven’t run into anything that wasn’t easily recoverable.
And that’s it!
Did you find this useful? Is there anything I missed?

Thursday, May 16, 2013

How to add path of a program to $PATH environment variable?



How to add path of a program to $PATH environment variable?



Firstly, check your original path:
echo $PATH
It should show something like this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Now add your program to that path, ensuring your using the entire path all the way from / to your program.
export PATH=$PATH:/path/to/my/program
This sets your PATH variable to the existing PATH plus what you add to the end. Check that it has been added:
echo $PATH