[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Please critique this script



First of all- apologies if this is the wrong list as this posting is about general perl coding style, however as it is also about a perl script that will only work under MacPerl I thought it less annoying to post 1 single (possibly badly directed) missive, rather than cross post several times generating multiple copies for those of you who are subscribed to more that 1 MacPerl list.

Any comments on the following code would be greatly appreciated and I also have a question,or rather 2.

Premise:
I keep seeing talk of subs being expensive in terms of performance, however my instinct is to modularise my scripts with subs. In the code example here, were I to include it in a script I would be tempted do this, as it would have nothing to do with what the script would use the pref files for. So my questions are:


Is my instinct perl-ish?
How expensive are subs? (intelligent comments welcomed, RTFPers (read the fu**ing pod) please refrain from troubling themselves to post as I can use my imagination in this regard, thank you very much :) )





#!/usr/bin/perl -w

#script checks for prefs folder and files in the sytem folder
#creating them if they don't already exist


use Mac::Files;
require "FindFolder.pl";
use strict;



my(%db_files,@defaults,$test,$file,$path,$entry,$error);
@defaults = qw ( U_A_Prefs
Prefs
User_DB );

no strict "subs"; #need this or compiler complains about breword "P"
$path=(MacPerl::FindFolder(P)); #finds the system prefs file
use strict;

if (!-e $path.$defaults[0]) {#check if folder exists in the prefs file

$error = mkdir($path.$defaults[0], 0755);#create folder if not there already

&handle if ($error==0);#check the return value for success (or not)

#and send failures to be handled


$path= "$path$defaults[0]:";#set the revised path
shift (@defaults);#delete the folder name from the array

foreach $file(@defaults) {#iterate through the file names
$error= FSpCreate( "$path$file", 'ttxt', 'TEXT') if (($test=(-e $path.$file)) = 0) ;#check for existence as per directory
&handle if ($error==0);#error handling
}


}else {#if folder already exists
$path= "$path$defaults[0]:";#set path to include the folder
$ENV{"PATH"}= $path; #put it into the ENV hash for accessing later
}




##--------sub routine separator

sub handle{#if errors have been thrown up say what by and kill the script
print "there was a problem creating file $file in folder $path\n";
print "script terminated\n";


exit;
}

_END_