Perl Scripts
This page provides various
Perl
scripts that I have written over the years. Most of them were written and tested
on Mac OS X, and some are specific for OS X,
but most should run on any platform where Perl is installed.
File & Folder Management
-
copy_without_xattr
Files on OS X can have resource forks and other extended attributes and
as of Tiger (10.4) the 'cp' command copies these extended attributes.
This script copies files to a specified destination
but suppresses the copying of extended attributes.
This is useful when the destination is on a non-HFS volume
in order to avoid the "._" files that would otherwise be produced.
This Perl script can be conveniently invoked from an AppleScript droplet:
copyWithoutXAttr.applescript
-
findDupeFiles
Attempts to identify which files (with a specified suffix) might be duplicates
by looking at the MD5 digests.
See this
macosxhints article for further discussion.
-
getFileInfo
Displays the "creator code" and "type code" of Macintosh files.
(See also 'setFileInfo' and 'show_app_creator_code')
-
grepl
Reproduces the functionality of 'grep -l'. (This is useful since there are some
patterns that are difficult with 'grep'.)
Illustrates how to go on to the next file when using the <> magic to
iterate over all lines in all input files.
-
listdir
Lists the files in the specified directory.
It gives the info from 'stat' (i.e. similar to 'ls -l')
-
listdir_seq
Lists the files in the specified directory
but shows numerical sequences as one item.
E.g. the files file1.txt, file2.txt, file3.txt would show as file[1-3].txt
(See also 'nextFilename')
-
nextFilename
Displays the name to be used for the next file in a specified directory
where the files are named in a numerical sequence.
E.g. if the files file1.txt, file2.txt, file3.txt exist,
the command 'nextFilename file1.txt' would output "file4.txt".
(See also 'listdir_seq')
-
peekFile
Displays the values of the bytes at specified offsets in a specified file.
(The name 'peekFile' is supposed to suggest the 'peek' command of BASIC)
-
pokeFile
Edits a binary file by replacing the bytes at specified offsets
with the supplied values.
(The name 'pokeFile' is supposed to suggest the 'poke' command of BASIC)
-
rename_case_colliders
Renames files where necessary so that the filenames are distinct
on case-insensitive filesystems (like HFS+ on OS X)
-
rename_for_windows
Renames files where necessary so that the filenames are acceptable to MS Windows
-
rename_without_endspace
Renames files where necessary to remove leading or trailing whitespace
in the filenames
-
reportLongPaths
Reports on files whose full paths are longer than a specified threshold.
This is useful when long paths are causing trouble when copying files to some
other device.
This script illustrates a simple use of the File::Find module.
-
resolveAlias
Resolves a Finder alias.
-
setFileInfo
Sets the "creator code" and "type code" of Macintosh files.
(See also 'getFileInfo' and 'show_app_creator_code')
-
show_app_creator_code
Displays the 4 character "creator code" for the specified application
-
show_binary
Displays the contents of files as base 2 numbers
-
splitdir
Splits a (presumably large) directory by moving its files
into several sequentially named sub-directories.
-
testFileCopy
Tests the speed of file copying - reports on the read, write, and transfer
speeds while copying a file from a specified source to a specified destination.
(This is intended for use when troubleshooting slow copying of files from a
networked drive for example.)
Sample usages are explained in the comments at the top of the script.
-
watchdir
Displays info when a specified directory/folder changes
-
watchfile
Displays info when a specified file changes
-
writeToTempFile
Writes specified text to a temporary file and outputs the name of that file.
This illustrates how to write to temp files in a secure way.
Generating Data
-
randBytes
Outputs (to STDOUT) a specified number of random bytes
-
sequentialBytes
Outputs (to STDOUT) a specified number of bytes that cycle from 0..255
Text Processing
-
commonStart
Determines the string that is the common start of a list of strings
-
combineCSV
Combines specified columns from several CSV files into one CSV file.
(If you are interested in CSV utilities, see also:
csvfix)
-
enumerateOccurrences
Reports on which out of a set of expected values occurred in the specified files
-
enumsToPerl
Creates a Perl module with the info from the 'enum's in a C/C++ header file.
Illustrates how to create a module where all the variables are automatically
exported.
-
letters
Shows the letters (in alphabetic order) that occur in specified strings.
-
permutations
Prints out all permutations of an array of strings
-
report_bad_macros
Checks on C pre-processor macros and reports macro definitions
that are not properly parenthesized
-
setOperation
Performs a set operation (union, intersection, etc) on data from files.
-
announceGremlins
Checks text files for the presence of gremlins.
(A "gremlin" is the BBEdit name for an unwanted character, often invisible,
that has somehow gotten into your text file.)
-
stripHtml
Outputs the contents of an HTML file after stripping off the HTML tags.
Illustrates how to use the HTML::TreeBuilder module.
-
usConstitutionSearch
Search the text of the US constitution
(as provided by www.usconstitution.net)
for a user-supplied search term and show the matching lines
with a few lines of surrounding context.
Could easily be modified to search any other text document.
Process Management
-
getProcessRss
Gets the "resident set size" of a specified process.
This provides a rough measure of how much RAM that process is using.
-
memhogs
Reports on the top RAM-consuming processes, swap-file usage and pageouts.
-
sudoTs
Reports on the 'sudo' timestamp
-
test_signals
Illustrates the effect of various
signals
Networking
-
get_wan_ip
Gets the "external IP address" by extracting it from the HTML
returned from a publicly available web site that provides this service.
User Interaction
-
ash (AppleScript shell)
Allows interactive use of AppleScript (like a Unix shell)
-
runScriptFromClipboard
Runs the script (shell script or AppleScript)
that is currently that is currently in the Clipboard
- i.e. the script text that had previously been copied to the Clipboard.
(see also 'runScriptFromWebPage')
-
runScriptFromWebPage
Runs the script (shell script or AppleScript)
that is currently selected on the frontmost web page
(see also 'runScriptFromClipboard')
-
simple_shell
An example of a very simple "shell" program - intended as an illustration of the
basic concepts
-
wait_for_keypress
Illustrates how to wait for a keypress in Perl
Misc
-
epochToIsoDate
Converts epoch time (seconds since January 1, 1970) to a date in
ISO 8601 format.
-
fifimage
An example of a
named pipe or FIFO.
Run this script and then read (e.g. with 'cat') from the fifo
and it will read a random jpg file from the image directory.
(See also 'logFifo')
-
getConsoleUsername
This script echos the name of the currently active user.
(this is useful when using Fast User Switching)
This script also serves as an illustration of using the 'scutil' command.
-
getDarwinSource
This script can be used to download the Darwin source code
(i.e. the open-source part of OS X) assuming you have an APSL account.
It illustrates the use of the Perl "LWP" module.
-
ioregValues
Reports the values of specified properties of an 'ioreg' object.
(The 'ioreg' command on OS X displays values of the "I/O Kit registry"
which includes a lot of information about the current state of the hardware
on an Apple Mac machine.)
-
logFifo
An example of a
named pipe or FIFO.
Run this script and then write (e.g. with 'echo') to the fifo.
Each message sent will be output to STDOUT tagged with the current time.
(See also 'fifimage')
-
lookieCookies
Shows info about the cookies that are in a Cookies.plist file
-
runEmbeddedScript
Illustrates how to run an embedded Perl script
(source embedded in the current script)
as a separate process. The new process (running the embedded Perl script)
is completely independent of the parent process.
It will continue to run even after the parent dies.
-
showLaunchdEnviron
This script shows the the environment (variables, etc) that is used
for scripts run via 'launchd'.
It also serves as an illustration of how to run 'launchd' scripts.
-
semaphoreMovie
Text to semaphore converter that creates a QuickTime movie showing the
semaphore flag
sequence for a given text string.
The flag images are derived from those on the above Wikipedia page.
Uses the
QuickTime command-line tools "qt_tools" to turn a sequence of images
into a movie. If you want to run this, get the
zip file with semaphoreMovie script and images.
-
speakPhonetic
Speaks text using the
NATO Phonetic Alphabet
-
test_stderr
Illustrates how text output can be sent to STDERR as well as STDOUT.
This script can be used to test output redirection in shell scripts
-
virusChecker
Purports to check for viruses - but in fact doesn't do anything other than
traverse the filesystem while reporting its progress
-
writingToFile
Illustrates how to read and write to files in Perl.
-
zero_separated
Outputs specified strings to STDOUT separated by zeros.
This is useful, for example, to generate a spelling dictionary
in the format apparently used by OS X's built-in spell checker.
(E.g. ~/Library/Spelling/en)