Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Wednesday, November 25, 2009

Perl script to exact packages from multi package Perl module

Here is a little script that I use to extract the individual modules from a multiple package Perl module.

While the script works, it isn't perfect and if you have any comments, have an addendum, etc, please let me know!

The script is below. Please make sure to edit $file, the input file, and $outdir, the destination directory. Good Luck.

#!/usr/bin/perl -w
use strict;
use File::Spec;
use File::Path qw(mkpath);
my $file = '/exact/path/to/perl/module.pm';
my $outdir = '/tmp/';
open(my $fh, '<', "$file") or die $!;
my $contents = '';
my $filename = undef;
while (<$fh>) {
if ( $_ =~ /^package (.*);/g ) {

# construct a new file
$contents = '';

# its filename
$filename = File::Spec->catfile( $outdir, split( /::/, $1 ) ) . '.pm';

# its contents
$contents .= $_;
} elsif ( $_ =~ /^1;/g ) {

# put the contents into $filename
$contents .= $_;
my ( $volume, $directories, $file ) = File::Spec->splitpath($filename);

# make the path
mkpath(
$directories,
{
verbose => 1,
mode => 0711,
}
);
do {
open OUT, ">$filename" or die "Can't write to $filename: $!\n";
print OUT $contents;
close OUT;
} if $filename;
} else {
$contents .= $_;
}
}
close $fh;

Monday, October 12, 2009

Callback Functions - Scoping

According to Wikipedia, a callback function is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

Callback functions are used very frequently in event driven software systems. Whenever an event happens, a function is called.

The idea is simple, but many people tend to become confused when using these things. The problem that they encounter is that people tend to run into problems with the scope of variables that they are using.

Part of the problem is remembering that the callback function may form a closure. When the callback function is executed, the scope consists of the callback function's code and references to any variables of the function containing the callback function reference. The converse is not true; the function containing the callback function reference cannot use the arguments and variables of the callback function.

Another thing that can confuse people using callback functions is the use of global variables. Since callbacks are usually called in response to events, it may not be clear what the current state of the system is and using any predefined global variable may result in inconsistent results.

In addition to global variables, variables using the same name in the code containing the reference to the callback function and with in the callback can sometimes confusing to people. In this case, the variable within the callback takes precedence.

# example function with closure

function makePowerTo(x) {
return function(y) {
var result = y;
for (var i = 1; i < x;i++)
result *= y;
return result;
};
}
var square = makePowerTo(2);
var cube = makePowerTo(3);
print(square(2)); // 4
print(cube(2)); // 8

square
and cube are both closures. They share the same function body definition, but store different environments. In square's environment, x is 2. As far as cube is concerned, x is 3.

Monday, October 05, 2009

Finding and Deleting Files

Today, I was trying to remove a bunch of temporary files using the DOS command prompt on Windows Vista. What a pain in the butt!

Let's assume that I have a bunch of unwanted ".tmp" files in the sub-directory "subdir". The directory "subdir" also has other sub-directories containing more files that I would like to delete.

For those of you familiar with Linux, to find and delete files its as easy as the following command:
find ./subdir/ -name '*.tmp'-exec rm -f {} \;
Unfortunately, I couldn't muster something like that up on Windows Vista. I tried stuff like
dir /B /S | find ".tmp"
This produced the full path of the files that I no longer wanted. When I then tried to pipe those files to del, like
dir /B /S | find ".tmp" | del
Vista just said 'The syntax of the command is incorrect.'

To make a long story short, I was finally able to come up with the following:
for /R %i in ("*.tmp") do del %i
Do you have an alternative way of finding and deleting files?

Monday, April 27, 2009

for loop using 'find'

I find myself constantly creating complex loops and forgetting, for a small instance of time, how to construct them again.

In this case, I wanted to list files that passed a certain criteria and loop over them so that I could call a Perl script on them.

For example:

for i in `find . -type f -name '*.pm' -print`; \
do perl some_script.pl $i; \
done
In the above case, I am searching for all files that end in pm, starting from the current directory (.) and once found, I am executing the perl script some_script.pl on them.

Of course, you don't have run a perl script ... you could just echo the output:

for i in `find . -type f -name '*.pm' -print`; \
do echo found $i; \
done
Now, next time I need to run a similar command, I wont forget how to construct the query!