Using Perl references for developing

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 live server. I stored each version of the applescript into a scalar variable. Then at the top of my script I set a variable called $deploy. I check $deploy to see if it is set to live or dev, putting a reference of the mount script into that variable. The subroutine then dereferences the mountscript and I get the Applescript I want. This way I can easily turn on dev or live with one variable at the top of the script. I don’t need to set up if else statements when I call the mountscript sub, it can contain the same argument scalar that dereferences at runtime.

##################

our $deploy = “dev”; # make it live or dev for testing or real

##################

#main package variables - need not be set manually

our $mountScriptDev = qq|
tell application “Finder”
–activate
try
mount volume “afp://10.1.4.220/fusionProJobs” as user name “me” with password “mypassword”
set mountvar to “FusionPro mounted”
on error
set mountvar to “mount failed”
end try
end tell
|;

our $mountScriptLive = qq|
tell application “Finder”
–activate
try
mount volume “afp://10.1.4.230/D” as user name “me” with password “mypassword”
set mountvar to “FusionPro mounted”
on error
set mountvar to “mount failed”
end try
end tell
|;

our $mountScript = ($deploy eq “live”) ? \$mountScriptLive : \$mountScriptDev;

##########################

#main package subroutines

sub AppleScript {
my($script) = shift @_;
system(qq|/usr/bin/osascript -e ‘$script’|);
}

sub mountDrive {
&AppleScript(${$mountScript})
}

Comments are closed.