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

[MacPerl] MAC Filesystem Help



According to Ameet Dhillon:
> 
> I am new to MacPerl and am having some difficulty finding information to 
<snip>
> ----------------------------------------------------------------------
> # temporary file which contains the name of all files in "Frads"
> system("ls c:/\Frads > temp"); # this needs to be converted to MAC
> 
> # open filehandle called "temp"
> open(temp,"temp"); 
> 
> # read each file name 1 by 1
> while ($direct = <temp>) {
> 
> 	# create path to file
> 	$fname = "c:\\Frads\\" . "$direct"; # convert to MAC
> 	
> 	# open filehandle to current file
> 	open(input,"$fname");
> -----------------------------------------------------------------------	
<snip>

I believe this is now answered in the FAQ someone is
keeping.  However, for a faster response...

1. On no system should you ever use the SYSTEM command
unless it is absolutely necessary.  This is because the
system command spawns off a subprocess which carries the
entire overhead of your log-in.  Further, it really opens
up your program to do nasty things you may not want.  :-)
Instead, use the OPENDIR, READDIR, and CLOSE commands to
read directories, the CHMOD command to change privileges,
RENAME to rename and move files, etc....  For example, your
system command can be re-written to read:

	opendir( THEDIR, "$theDirectory" ) || die $!;
	@theList = grep( !/^\.\.?$/, readdir(THEDIR) );
	closedir( THEDIR );

which would allow you to read in a directory listing from
any directory.  All you have to do is to set $theDirectory
to where you want to read from.

2. Instead of hard coding all of the paths into a program,
I recommend using a subroutine to set the paths.  This is
because you can create a subroutine which you then call
repeatedly when you want to set any pathname.  Here is a
routine I wrote some time ago.  It is called PP or Proper
Path.

#
#   -------------------------------------------------------------------
#   Proper Path routine.
#   -------------------------------------------------------------------
#
sub pp
{
    local( $fileName ) = @_;

    my $theDirectory = "Duck:Quack";

    if( $^O eq "MacOS" ){
        return "$theDirectory:$fileName";
        }
        elsif( $^O eq "IBM" ){
            return "$theDirectory\\$fileName";
            }
        else {
            return "$theDirectory/$fileName";
            }

    return -1;
}

I place this routine into the directory where I have my
programs.  I then edit the file and set the variable
$theDirectory to whatever directory it is I'm working out
of.  Then when I want to read or write to a file I just
call this routine once for each file and it sets the proper
path for me and I don't have to remember to put colons or
backslashes or forward slashes between the directory name
and the file name.

The above could also be modified so that $theDirectory just
does a PWD (which returns the current directory).  This
would make it more flexible but at the same time more
dangerous.  Since, if you were starting the program from a
sensitive directory, you could wind up messing things up
without realizing it.  Which is why I hard code the
directory.

An example of using the above would be:

	$theLock = &generic::pp( "user.lock" );

which would return $theLock a value of:

	"Duck:Quack:user.lock"

on a Macintosh,

	"Duck:Quack\user.lock"

on an IBM (which of course would be wrong and I'd have to
edit the $theDirectory variable to correct this), and

	"Duck:Quack/user.lock"

on any other kind of system.  Which (again) would acutally
be wrong and I'd first edit the $theDirectory variable.

The nice thing about this routine is that you only have to
set the directory once.  From then on you just use the PP
routine.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch