Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Thursday, October 16, 2008

MySQL DBD in Activestate

I'm currently doing some consulting work to bridge the gap between Oracle and MySQL. Being in a Windows environment, I'm using the Activestate Perl distribution. Fortunately, it includes a nice ODBC module for connecting to Oracle out of the box. The bad news is, it doesn't contain a MySQL module. Luckily, I found a third party host that does provide the module. The following ppm (Perl Package Manager) one-liner takes care of the issue.

C:\>ppm install http://cpan.uwinnipeg.ca/PPMPackages/10xx/DBD-mysql.ppd

Tuesday, August 26, 2008

Perl Broken on CentOS 5.2 and Fedora 9

I'm not really a fan of re-blogging stuff, but I found this so staggeringly stupefying that I felt the need to post it here.

Redhat Perl, What A Tragedy

We run a variety of Perl scripts on our production systems here. A quick test using the script from the article shows that a basic loop calling bless 50,000 times takes 0.16 seconds on my Ubuntu workstation. Meanwhile, our uber-powerful production server, which runs a Redhat derivative, takes over four seconds to run the script. Our development server, running the same OS on slightly less powerful hardware took 24 seconds.

To summarize, if you run CentOS or Fedora, and you use Perl in production, you may have a problem.

Tuesday, May 27, 2008

Running Out of Space?

Running out of space on your HD? I am on a few of my machines. I found the following small Perl script that does a great job helping me locate redundant files. It recurses through a given root directory and looks for files of the same size. When it finds two files of the same size, it does an md5sum on them to determine if they're duplicates. It runs pretty quickly and is easy to tweak to pipe into other scripts.

Find duplicate files.

Friday, May 2, 2008

Finding Recent SVN Commits

Here's a quick Perl script I whipped up to ease finding newly committed files in our subversion repository. It's a little cleaner than what the client provides and nice to quickly find changes.

#!/usr/bin/perl -w

#
# quick script to succinctly display SVN changes since a given date
# usage: svnsince.pl YYYY-MM-DD HH:MM
#

use strict;
use Date::Calc qw(Date_to_Time);

if (!defined($ARGV[0]) || $ARGV[0] !~ /^\d\d\d\d-\d\d-\d\d$/) {
die("Invalid date. Usage: $0 YYYY-MM-DD HH:MM.");
}

if (!defined($ARGV[1]) || $ARGV[1] !~ /^\d\d:\d\d$/) {
die("Invalid time. Usage: $0 YYYY-MM-DD HH:MM.");
}

my $date_thresh = date_to_epoch($ARGV[0], $ARGV[1]);
my $svn_file = "";
my $svn_date = "";
open(SVN, "svn info -R |") || die("Can't run svn client: $!");

while (<SVN>) {
if (/^Path: (.+)/) {
$svn_file = $1;
}

if (/^Last Changed Date: (\d\d\d\d-\d\d-\d\d) (\d\d:\d\d)/) {
$svn_date = date_to_epoch($1, $2);
if ($svn_date >= $date_thresh) {
print "$1 $2 $svn_file\n";
}
}
}
close(SVN);

sub date_to_epoch {
my ($date, $time) = @_;
my ($year, $month, $day) = split(/-/, $date);
my ($hour, $minute) = split(/:/, $time);
return Date_to_Time($year, $month, $day, $hour, $minute, 0);
}

Friday, March 21, 2008

Perl Mass Substitution

Here's a nice trick I've used many many times over the years. Say that you have a group of files, and you want to replace some text in every file quickly and easily. Perl makes this really easy, and it's ubiquity pretty much guarantees it's around if you're on a *nix platform. For the sake of example, we'll pretend we have a group of html files, and we want to change every occurrence of index.html to index.php. The syntax is as follows:

[edited - thanks Chris!]

perl -pi.bak -e 's/index\.html/index.php/gi' *.html

The command line options provided break down as follows:

-p assumes a while loop over each line of every file and implicitly prints
-i specifies that you want in-place substitution on the file (no redirection of STDOUT required)
-i also takes an option argument of a backup file extension (.bak in this case)
-e tells perl to run the following code on the command line

From there, you're using standard Perl regular expressions. Don't worry if you're not a regexp guru. In the simplest form, you can simply replace one string with another.

The Perl code breaks down as follows:

s(means substitute)/string you want to match/string to replace/
(g means replace multiple instances per line)
(i means case-insensitive matching... aka ignore uppercase and lowercase differences)

Of course if you know regular expressions, you can do all kinds of fancy stuff.

Here's a nice reference.

Monday, January 28, 2008

Using Perl

If your install of Vim was built with an embedded Perl interpreter, you can leverage the power of the language to your advantage. I typically use the embedded Perl interpreter to add the power of Perl's regular expressions to Vim. Say you're editing a file, you make a selection in visual mode, and you want to substitute all digits with the `x' character. This is easily accomplished using the perldo ex command.

:perldo s/\d/x/g

See http://perldoc.perl.org/perlre.html and :help perldo for more information.