Tuesday, 30 September 2008

Optionally using Smart::Comments

Finally figured out how to turn Smart::Comments on and off as command line arguments:


shell> cat if.pl
#! /usr/bin/env perl

use 5.010_000;
use if
qr{\A -+ smart \z}ixms ~~ @ARGV,
'Smart::Comments' => '####', '###';

#### Going to sleep for intervals:
for ( 1 .. 5 ) { ### Speeping ... [%] Done
sleep 2;
}

exit 0;
shell> ./if.pl --smart

### Going to sleep for intervals:
Speeping ........ [50%] Done


You can also get the same effect like so:


...
use if
$ENV{SMART},
'Smart::Comments' => '####', '###';
...

shell> SMART=1 ./if.pl

Friday, 26 September 2008

shebang line

Don't use
#! perl
Use
#! /usr/bin/env perl
or
#! /actual/path/to/perl
This is because the first version cannot be executed like so:
shell> ./your_perl_program.pl
You must do:
shell> perl your_perl_program.pl

Net::FTP ... be careful with modes

Took ages for Sam and I to figure out why the files we were downloading could not be opened. It was a simple case of not specifying the format for Net::FTP to transfer our files in.


#! /usr/bin/env perl

use 5.010_000;

use strict;
use warnings;

use Archive::Extract;
use Net::FTP;

my $data_file = shift;
my $outputdir = shift;
my $ftp_wtccc = Net::FTP->new(
Host => 'ftp.sanger.ac.uk',
Debug => 0,
Timeout => 300,
Passive => 1,
) or die 'Could not connect to ftp.sanger.ac.uk';

$ftp_wtccc->login( "********", "********" )
or die "Cannot login ", $ftp_wtccc->message;

if ( grep {m/$data_file/xms} $ftp_wtccc->ls ) {
$ftp_wtccc->binary;
$ftp_wtccc->get($data_file) or die "Could not get $data_file";
}

$ftp_wtccc->quit;

my $archive =
Archive::Extract->new( archive => $data_file )
->extract( to => $outputdir // '/tmp' );

exit 0;

Wednesday, 24 September 2008

The evilness of empty files

I am learning the hard way that it does pay to check that your programs do output what you expect every time they are run! In this most recent case, I have submitted jobs to the "farm" and have had a whole bunch of files created. Trouble is, some of these files are empty =(

Next time, at the end of every function that outputs to a file, I should put a check to ensure the file is not empty ... except in explicit cases where that is an expected outcome:

sub save_manipulation_in_outfile {
...
if ( ! -s $outfile ) {
confess "Output file ($outfile) is empty"
}
return $outfile
}