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

Re: [MacPerl] Using UNIX style filenames with MacPerl



Josh Kuperman wrote:

> (snipped)
> networks. Mostly I just want to be able to write something that will be
> taken to be  ./dir1/fn2 or dir1/fn2 on unix and :dir1:fn2 on the Mac. I
> would prefer it to look like the Unix.

Well, if we're just talking about the directory separator (":" for MacOS
and "/" for UNIX), there is a solution to making the codes portable
without modifying the codes. This may not be the best solution, but it
should be simple enough. 

Include the lines below in your program that you have to use both on Mac
and UNIX. 

__example__
# The line below check the OS and assign the appropriate dir separator
($^O =~ /MacOS/) ? $DS = ":" : $DS = "/"; # $DS holds the separator

# Wherever the path is hardcoded, use $DS like so:
$my_dir = $DS."usr".$DS."home".$DS."userid"; 
# $my_dir will be ":usr:home:userid" on MacOS, and "/usr/home/userid" on
others
__end__

The key here is the special variable $^O. It holds the name of the OS
the Perl interpreter runs on. On Mac, it holds "MacOS", on Solaris
"solaris", Linux "linux", BSDI "bsdos", FreeBSD "freebsd", and Windows
"Win32" (Windows case is my guess :) ). 

So, if you need to run your perl programs on Mac, UNIX, and even
Windows, you can write your program like the example below to avoid
having to write three different versions (of course it is *only* if
you're dealing with the directory separator. If you're using some other
platform-specific codes, such as modules, system calls, etc., then it
take a lot more). 

__example__
# To avoid worrying about the dir separator...
if ($^O =~ /MacOS/) { $DS = ":"; }
elsif ($^O =~ /Win/i) { $DS = "\\"; } # backslash needs to be escaped
else { $DS = "/"; } # assuming it's one of UNIX

# Now I can hardcode any files I need to use
$file1 = "dir1".$DS."file1";
$file2 = "dir2".$DS."file2";
...
__end__

Now, you may think that it is cumbersome to rewrite your codes you've
already written. Well, if you use BBEdit (great stuff!), use the
excellent Replace & Find Again command. If you use vi, then use the ex
command ":%s/:/.$DS./gc" for Mac-oriented codes or ":%s#/#.$DS#gc" for
UNIX codes. 

Hope this helps. 

--Akira Hangai

-- 
A penny saved is ridiculous.

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