Reuse and maintenance prompts refactoring.
package BWright::FNP; #-------------------------------------------------------------------- # PACKAGE/MODULE DEFINITION # Set up the package, export public methods/variables # Return true for the package #-------------------------------------------------------------------- require Exporter; @ISA = qw( Exporter ); #Default for PM #Export any necessary vars. @EXPORT = qw( makePerlPath makeWindowsPath makeGlobPath getFileList getDirectoryList getFullDirectoryList getFileName getPrefix getSuffix writeFile readFile ); 1; #Return true Default #-------------------------------------------------------------------- # PACKAGE FUNCTIONALITY # This is where the custom module functions go #-------------------------------------------------------------------- #getFileName sub getFileName($file) { my $file = $_[0]; @sections = split(///,$file); return $sections[$sections - 1]; #Make c:imageName.jpg into just imageName.jpg. } #getPrefix sub getPrefix($file) { my ($file) = (@_); @sections = split(///,$file); @prefix = split(/./,$sections[$sections - 1]); return $prefix[$prefix - 2]; #Make c:imageName.jpg into just imageName } #getSuffix sub getSuffix($file) { my ($file) = (@_); @sections = split(///,$file); @suffix = split(/./,$sections[$sections - 1]); return $suffix[$suffix - 1]; #Make c:imageName.jpg into just .jpg } #getDirectoryList sub getFullDirectoryList($path, $int) { my ($path, $int) = @_; @files = glob(&makeGlobPath(&makePerlPath($path))); if ($int == 1) { foreach $file (@files) { print $file."n"; } } return @files; } sub getFileList($path, $int) { my ($path, $int) = @_; @files = glob(&makeGlobPath(&makePerlPath($path))); @_testFiles = @files; @files = (); foreach $file (@_testFiles) { if (-f $file ) { push(@files, $file); if ($int == 1) { print $file."n";} } } return @files; } sub getDirectoryList($path, $int) { my ($path, $int) = @_; @files = glob(&makeGlobPath(&makePerlPath($path))); @_testFiles = @files; @files = (); foreach $file (@_testFiles) { if (-d $file ) { push(@files, $file); if ($int == 1) { print $file."n";} } } return @files; } sub makeGlobPath { my ($path) = @_; $path =~ s/ /\ /g; #Correct Path Errors by escaping spaces. #C:/Documents and Settings/* will become C:/Documents and Settings/* #Append the characters '*' to enable the glob $path =~ s//$//*/g; return $path; } sub makePerlPath { my ($filePath, $isDir) = @_; $filePath =~ s/s+$//g; #Remove Trailing Spaces $filePath =~ s/\///g; #Replaces the default windows separator, the backslash, with the forward slash if ($isDir == 1) { $filePath =~ s/(w$)/$1//g; #Append a forward slash to the end of the path if it isn't already present. (IE, if the line ends in a w) } return $filePath; } sub makeWindowsPath { my ($filePath, $isDir) = @_; $filePath =~ s/s+$//g; #Remove Trailing Spaces $filePath =~ s///\/g; #Replaces forward slash in path with backslash return $filePath; } sub writeFile($filePath, $fileContents) { my ($filePath, $fileContents) = @_; # Write Macro open($MYFILE, $filePath); print $MYFILE $fileContents; close($MYFILE); return 1; } sub readFile($filePath) { open (FILEHANDLE, $filePath); @file_contents = <FILEHANDLE>; close(FILEHANDLE); return @file_contents; }

