Archive for the 'Perl' Category

Building a list using perl

Tuesday, December 23rd, 2008

In simpleist terms. Here is a perl script to build a list from a directory.

#!/usr/bin/perl
opendir(DOCS1, "/Users/bradrice/Documents/UAworkFolder/stadium/cams/slideshow/images/quot;);
my @files = grep !/^\.\.?/, readdir DOCS1;
foreach $file(@files) {
print $file . “\n”;
}

Using smb and rsync to backup

Monday, January 28th, 2008

I needed a script to backup a workfolder to a Windows network server. I wrote this perl script that can be triggered using cron. First create a mount point. I used /smb/public. So to do so in two steps, mkdir /smb, then mkdir /smb/public. I put my scripts in ~/bin. Put this perl script below [...]

/usr/bin/head and perl lwp

Tuesday, July 24th, 2007

I use cpan to install perl modules all the time. I recently installed Perl LWP. Unfortunately the install replaces and important text command file in /usr/bin. The file head was replaced by LWP head and this has caused some problems on my system. I found it out when I tried to install Sun’s Netbeans IDE [...]

Using Perl to build a table of images

Monday, July 16th, 2007

I had a very large folder of images that I needed to build a tale of, as well as links for the images. I wrote a perl script to read the directory and build the table based upon the image file names in the folder. For our purposes here I am simplifying the script.

#!/usr/bin/perl
use File::Copy;
my [...]

listing all form input names

Tuesday, April 10th, 2007

Ever writing code and need to get a list of the input field names in your html document. I wrote this little perl script to parse out a list of names of the input fields.

#!/usr/bin/perl
open (INFILE, ‘
while ($line=) {
chomp $line;
my $input = $line;
if ($input =~ /input /
$input =~ /name=\”(\w*)\”/i;
$input = $1;
print “$input\n” ;
}
}
How it [...]

regex for path names

Saturday, February 24th, 2007

May seem simple to most folks, but just in case you are looking for an expression to remove the path before a file name here is an expression to start.
[\w/]*/
It searches for all the word characters before the last slash. If you need periods in the path add those to the character class.

Using Perl references for developing

Wednesday, January 3rd, 2007

I found a good way to make use of a scalar reference in perl. For development when you are using a dev and live server, you can change some settings using a ref.
Here’s what I did recently. In this case I needed two different mount scripts, one for a dev server and one for a [...]

Doing the Perl curl

Thursday, July 27th, 2006

A while back I wrote a post about a ftp shell script. A number of people told me I should use curl instead. Here is one I wrote using both perl and curl to move some files from a site. I start with a list of file names. They don’t have the .pdf extension in [...]

Sorting file names with Perl

Monday, July 24th, 2006

I had to sort directories of pdf files that had identical names except for the last characters before the extension.pdf. Perl has a nice built in sort routine, but when I used it to sort like this:
@sortedlist = sort @listOfFiles;
I would end up with filenames in sort order for the most part except 10 would [...]

Using Perl to change file names

Wednesday, March 29th, 2006

I run into the situation of having to rename so often that I am constantly tweaking my solution. Here I had to remove some extraneous characters on file names that a client had added. All we wanted where the first characters that started with DSFC and then the 4 digits. I had to drop off [...]

Trusty Applications

Saturday, March 11th, 2006

I have several handy applications that I use on my mac daily. Obviously a mail app and browser. Safari is very nice. I’m using Apple Mail, but Thunderbird is really a nice mail app, too. But for me, much of the business of computing is storing information, and being able to get to that information [...]

Using Perl to compare two directories

Friday, December 16th, 2005

If you ever need to compare two directories to find files missing from one that are in the other, perl can be a good utility to use. You can read into two arrays two directories, then create a hash using the second directory array as keys (this should be the smaller directory). Then use the [...]

Perl move files script

Wednesday, March 9th, 2005

Here is a little script I wrote that you can use to move a list of files. Supply it a list of file names. In this case the file names I was supplied didn’t have .pdf on them, so I had to concatentate that onto the file names. I’ve covered this earlier with respect to [...]

Change case

Friday, July 23rd, 2004

Here is a little perl script that you can use to lowercase text with an uppercase first letter of each word. If you put it in your BBEdit unix filters it will work on any selected text.
#! perl -w
use strict;
while () {
tr/[A-Z]/[a-z]/;
s/\b([a-z])/\u$1/g;
print $_;
}

Perl File::Copy and Apple’s ditto command

Wednesday, July 21st, 2004

You can use Perl’s File::Copy module to move files around your system. The only drawback is that if the files are macbinary and have a resource fork, that portion of the file gets lost. You can use Apples ditto command with –rsrc parameter to move files and maintain the resource.
Following is a perl subroutine I [...]

Using Applescript with PERL

Sunday, July 18th, 2004

One of the great advantages of OS X is that you can control applications using Applescript. There is a command line version of calling applescript called osascript. The really cool thing is being able to use this command inside of another script such as a perl script or a shell script. One of the things [...]

Using Perl to change file names

Friday, July 16th, 2004

The previous tip showed how you can change filenames in a given directory uisng the command line. If you need to use perl to programatically change file names you can use the rename perl function. Often times I need to iterate through a directory and change names of files based on a given condition. Here [...]