scripts.team-holm.net
Hovedsiden
Perl Scripts
Handy Subs


Including some handy sub routines or code snippets that can be easily re-used.

alwaysRoundUp.pl
#!/usr/bin/perl

#26.12.2011 - Atle Holm
#Always rounds a number up, no matter how close it is to round down

use POSIX;
use warning;
use strict;

print round(3.1);

sub intCheck{
   my $num = shift;
   return ($num =~ m/^\d+$/);
}
sub round {
   my $var = shift;
   if (intCheck($var - 0.5)) { $var = $var + 0.1; }
   return ceil($var);
}

checkTimeInterval.pl
#!/usr/bin/perl

#26.12.2011 - Atle Holm
#Check a time interval

use Date::Parse;
use Date::Format;
use warning;
use strict;

my $updateInterval = (60 * 60 * 24 * 7); #7 days in seconds
my $oldDate = getSomeExampleDateFunction();

#If the difference is more then seven days, then print
my @lt = localtime(time);
if(((str2time(strftime("%c\n", @lt))) - str2time($resultDateReturn)) > $updateInterval) {
#If using MySQL; Could be replaced with: SELECT (now()-updated_at) FROM tableName;
   print "Difference is more then seven days!";
}


Perl
VisualBasic
BASH
Powershell