Author:
erics , October 10th, 2011
To round up to the nearest 5:
use Math :: Round qw ( nhimult ) ;
$ roundedUpNumber = nhimult ( 5 , $ originalNumber ) ;
http://search.cpan.org/dist/Math-Round/Round.pm To round up to the nearest 1: Cross-posted from PerlMonks:
sub roundUp {
my $ n = shift ;
return ( $ n == int ( $ n ) ? $ n : int ( $ n + 1 ) ) ;
}
Categories: How-To's , Technology Tags: Format , howto , Number , perl , Round , Rounding , tips
| No comments
Author:
erics , September 4th, 2011
Normally, I have to specify the desired repo on the command line: yum –enablerepo=remi,remi-test install {package name here} To enable the Remi repo so you do not have to specify it on the command line: # perl -pi -e ‘s/enabled=0/enabled=1/g’ /etc/yum.repos.d/remi.repo To disable the Remi repo so you do not have to specify it on […]
Categories: How-To's , Technology Tags: enablerepo , howto , perl , Remi , Repo , tips , vi , vim
| No comments
Author:
erics , August 19th, 2011
A small step forwards in understanding how Perl handles hashes as arguments. This biggest difference is that the first way “slurps” in ALL passed values in @_ into the %args hash. The second (cooler) way, pulls in the arguments hash as a single scalar variable, allowing multiple variables to be passed via @_, if you […]
Categories: How-To's , Technology Tags: Argument , Hash , howto , perl , Subroutine , tips
| No comments
Author:
erics , August 3rd, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl -w
use XML:: Simple ;
use Data:: Dumper ; # used for debugging the XML
use DBI ;
use DBIx:: SQLEngine ;
use strict ;
# Use KeyAttr to massage the xml into a proper hash
my $xs = XML:: Simple-> new ( KeyAttr = > { value = > 'type' } , forcearray = > [ 'value' ] ) ;
my $parsed ;
eval {
$parsed = $xs -> XMLin ( $xml ) ;
1;
} or do {
die "Error loading xml: $@" ;
} ;
#print Dumper $parsed; # DEBUG to see parsed XML
my $dsn = join ( ':' , qw ( DBI mysql { DATABASE_NAME } localhost 3306 ) ) . ':' ;
my $dbh = DBIx:: SQLEngine-> new ( $dsn , '{MYSQL_LOGIN}' , '{PASSWORD}' ) || die "Database connection Error: $DBI::errstr\n" ;
my $rows = $parsed -> { 'item' } ;
eval {
$dbh -> do_bulk_insert (
'table' = > 'myTable' ,
'values' = > $rows ,
) ;
1;
} or do {
die qq | SQL do_bulk_insert Crashed : $ @ | ;
} ;
exit 0;
Categories: How-To's , Technology Tags: mysql , perl , XML
| No comments
Author:
erics , August 3rd, 2011
$ string = ~ s / ^ \ s + //; # Left Trim
$ string = ~ s / \ s + $ //; # Right Trim
Categories: How-To's , Technology Tags: howto , ltrim , perl , rtrim , tips , trim , white space , Whitespace
| No comments
Author:
erics , June 29th, 2011
This simple formula will give a Y2K-compliant 4-digit year with low overhead:
$ year = ( localtime ) [ 5 ] + 1900 ;
Categories: How-To's , Technology Tags: howto , localtime , perl , tips , Y2K , Year
| No comments
Author:
erics , May 31st, 2011
I found myself needing a specific doctype the other day and this is what I found… First, get the correct doctype string from the W3C website list. Then, simply use -dtd to specify it to CGI.pm during the call to start_html:
- dtd = > '-//W3C//DTD HTML 4.01 Transitional//EN'
- OR -
- dtd = > [ '-//W3C//DTD HTML 4.01//EN' , 'http://www.w3.org/TR/html4/strict.dtd' ]
In addition, use -encoding to specify the character set for XHTML. It defaults […]
Categories: How-To's , Technology Tags: CGI.pm , DocType , DTD , encoding , howto , ISO-8859-1 , Loose , perl , Strict , tips , Transitional , UTF-8 , W3C
| No comments
Author:
erics , May 28th, 2011
use Date :: Manip ;
my @ months = qw / Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec / ;
my $ month = $ months [ ( localtime ) [ 4 ] ] ;
my $ last = & UnixDate ( "last saturday in $month" , "%B %e" ) ;
$ last . = & gensuffix ( $ last ) ;
sub gensuffix {
my ( $ val ) = @ _ ;
my $ end_num = substr ( $ val , - 1 , 1 ) ;
my $ suffix = 'th' ;
$ suffix = 'st' if $ end_num == 1 ;
$ suffix = 'nd' if $ end_num == 2 ;
$ suffix = 'rd' if $ end_num == 3 ;
return $ suffix ;
}
Categories: How-To's , Technology Tags: Date , Date::Manip , localtime , perl , Suffix , UnixDate
| No comments
Author:
erics , May 18th, 2011
use Tie :: IxHash ;
my $ myHash = { } ;
tie ( % { $ myHash } , 'Tie::IxHash' ) ;
$ myHash -> { 'a' } = 'apple' ;
$ myHash -> { 'b' } = 'blueberry' ;
Tie:IxHash CPAN Manual Page
Categories: How-To's , Technology Tags: Hash , IxHash , perl , Tie , Tie::IxHash
| No comments
Author:
erics , May 15th, 2011
Coming from the Perl world, I wanted to emulate the following code in JavaScript:
my $ myHash = {
'foo' = > 'Hello' ,
'bar' = > 'World' ,
} ;
foreach my $ myKey ( keys % $ myHash ) {
print $ myKey , '=' , $ myHash -> { $ myKey } ;
}
What I found is that JavaScript uses “Objects” to contain hash-style data:
// Define the hash object on the fly
var myHash = {
foo : "Hello" ,
bar : "World"
} ;
// loop through all keys
for ( var myKey in myHash ) {
alert ( myKey + '=' + myHash [ myKey ] ) ;
}
// access a key individually
alert ( 'foo=' + Hash . foo ) ;
alert ( 'bar=' + Hash . bar ) ;
Also, one may use variables as key names by using the square-bracket notation, which evaluates variables first:
var myVar = 'test' ;
//These two lines are equivalent as long as myVar is 'test':
myHash [ myVar ] = 'theValue' ;
myHash . test = 'theValue' ;
Categories: How-To's , Technology Tags: array , Hash , Iterate , javascript , Keys , Loop , Object , perl
| No comments