13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

APPENDIX ■ PERL TIPS• Date::Parse: Converts a string time to epoch seconds (which is the format thatPerl uses in most situations but which isn’t very human-readable).use Date::Parse;my $time = "2009-07-10 10:57:00";my $secs = str2time($time);It’ll also parse regular language dates (e.g., “10th July 2009 10am”), and it tries toget your local time zone right.• DBI::* : Provides a huge quantity of various database independent interfacemodules. There are many, many options and possibilities, but here’s a briefexample of the DBD::MySQL module (note that you need only to use DBI() to get thismodule):#!/usr/bin/perl -wuse strict;use DBI();# Connect to the database.my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","jkemp", "mypassword",{'RaiseError' => 1});# Create a new table 'mytable'. (No errorcatching, because this must not fail.)$dbh->do("CREATE TABLE mylist (id INTEGER, item VARCHAR(200))");# INSERT some data into 'mylist'. $dbh->quote() is used to quote the string.$dbh->do("INSERT INTO mylist VALUES (1, " . $dbh->quote("first list item") .")");• File::Find: Traverses a directory tree.use File::Find;my $directory = "/home/jkemp/";find ( \&wanted, $directory );sub wanted() {# here's where you specify which sorts of# files you want to be found}• File::Glob Extends standard file globbing to use BSD-style globbing (the Perlbuilt-in uses csh-style globbing). Most notably, this handles whitespace inpatterns better.• Getopt::Long: Handles command-line options in your script, if you havecomplicated options. Here’s a simple example:use Getopt::Long;my $verbose = '';GetOptions ('verbose' => \$verbose);249Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!