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
-
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
-
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
-
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
-
enumerateOccurrences
Reports on which out of a set of expected values occurred in the specified files
-
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.)
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.
-
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')
-
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
-
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.
-
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
-
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)
-
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.