Novell Home

Cool Perl Tricks

From Developer Community

Contents

Basic Mysql Connection/queries

my $conn = DBI->connect("dbi:mysql:database=<DATABASE NAME>;host=<HOST URL>;user=<USER NAME>;password=<USER PASSWORD>") ||
	die ("Couldn't connect to database. ".$DBI::errstr);

my $sql = qq{
	<SQL HERE>
};
	
my $stmt = $conn->prepare($sql);
$stmt->execute();
	
my $dataRef = $stmt->fetchall_hashref(<FIELD TO KEY ON>);

if (($dataRef->{'<KEY>'}->{'<DESIRED FIELD>'}) =~ /enum(\(.*\))/)
{
	. . .
}

$conn->disconnect();	

SQL

my $sql = qq{
		<query here>
	};

grep

grep { <code here> } <a list here>

split pop|shift

shift @{[split /\//, $filePath]}

expand a function call in a string without concat

This is useful because it allows you to call functions inside of regular expressions

print "The time is ${ my $x = localtime; \$x }

This is taken from the Perl FAQ #4

Novell® Making IT Work As One

© 2009 Novell, Inc. All Rights Reserved.