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

Re: [MacPerl] Platform checking



   Mime-Version: 1.0
   Content-Type: text/plain; charset="us-ascii"
   Date: Wed, 13 Mar 1996 08:34:46 -0800
   From: russt@ccnet.com (Russ Tremain)
   Sender: owner-mac-perl@iis.ee.ethz.ch
   Precedence: bulk

   Interesting discussion - I've been examining the @INC variable to see which
   system i'm on...Does anyone have a general method that will work on dos,
   nt, vms, unix, and the mac, that is independent of perl revision?
   /russt

Here is the current version of something that I posted a couple months ago.
(People had some great comments - I really appreciated the 'design review'.  I
regard this list as one of the best (technically) that I subscribe to.  Thanks
for listening and for spending the time helping.)

Unfortunately, I have not tried this at all on any flavor of perl5...

thanks,
-sandy

# for cross platform development on the mac, initial some stuff
# Note: the config code is shamelessly snarfed from:
#
# ssUtilities.pl
# Package of general purpose utilities. 
# Sandra Silcot, Jan 1995, ssilcot@www.unimelb.edu.au
# Changes:
#     11 Apr 95 - bugfix - force scalar value to be passed to 'MakePath
#
CONFIG: {
    if ($MacPerl'Version) {    #' are we in MacPerl
        $mac=1; $unix=0; $nt=0;
        $pd = ':';              # use this in pathname pattern matching (mac)
        $MacQuitLevel = 0;      # quit level: 0 - don't quit after script
                                # 1 - quit if in run time
                                # 2 - always quit
                                # 3 - quit if this is the first script since running perl
        # require these two guys since they almost always used...
        require "StandardFile.pl";   # includes GUSI
        require "FileCopy.pl";  # defines MacPerl'FileCopy
        # get startup volume
        $ENV{'HOME'} = &MacPerl'MakePath(scalar(&MacPerl'Volumes()));
        chop($ENV{'HOME'});    # startup disk with no trailing colon
        $ENV{'PWD'} = `pwd`;   # set env var to current working dir
        chop($ENV{'PWD'});     # get rid of the return at the end
        # customise these as required
        $default_mac_creator = 'SANS';
        $default_mac_type    = 'TEXT';
        $ENV{'PATH'} = "";
        $ENV{'USER'} = `hostname`;
        chop($ENV{'USER'});
    }
    elsif ($] !~ /NT/) {
        # unix
        $unix=1; $mac=0; $nt=0;
        $pd = '/';
        # from unix, PATH is set from both csh and ksh.  If it is not set and path is,
        # set it
        $ENV{'PATH'} = $ENV{'path'} if (!defined($ENV{'PATH'}) && defined($ENV{'path'}));
        # sanity check...
        die "Initialization script (CONFIG) failed - unix/nt confusion\n"
            if ((! -e "/usr") ||        # unix environs have a /usr
                # yuck - some NT shells uppercase comspec and some
                # capitalize it...
                (($tmp = $ENV{'COMSPEC'}) && ($tmp =~ /^[a-zA-Z]:\\/) && (-e $tmp)) ||
                (($tmp = $ENV{'ComSpec'}) && ($tmp =~ /^[a-zA-Z]:\\/) && (-e $tmp)));
    }
    else {
        # nt
        $nt=1; $unix=0; $mac=0;
        # perl on NT still wants the directory separator to be "/"
        # some NT shells want it to be "/" likewise; however, cmd.exe
        # may want it to be "\" - be careful...
        $pd = '/';
        # for the NT, path is ; separated and includes drive letters...
        # for example: $ENV{'PATH'} = "h:/atria/bin;c:/mksnt;t:/coretools/bin";
        # Note: if ComSpec is set to mks, must run pwd; if not, can run cd
        if ((($tmp = $ENV{'COMSPEC'}) && ($tmp =~ /cmd\.exe$/i)) ||
            (($tmp = $ENV{'ComSpec'}) && ($tmp =~ /cmd\.exe$/i))) {
            $ENV{'PWD'} = `cd`;
            $ENV{'PWD'} =~ s|\\|/|g; # make sure that PWD is in '/' syntax
        }
        else {
            $ENV{'PWD'} = `pwd`; # will use '/'
        }
        chop($ENV{'PWD'});
        # sanity check...
        die "Initialization script (CONFIG) failed - nt/unix confusion\n"
            # do not check for /usr here - on the NT checking for (-e "/usr")
            # checks for "/usr" on the current drive - multi-rooted file systems
            unless ((($tmp = $ENV{'COMSPEC'}) && ($tmp =~ /^[a-zA-Z]:\\/) && (-e $tmp)) ||
                    (($tmp = $ENV{'ComSpec'}) && ($tmp =~ /^[a-zA-Z]:\\/) && (-e $tmp)));
    }
    # something to record the current working directory ($pd has been defined at this point)
    $ENV{'PWD'} =~ s|$pd+$||;   # make sure that it does not end with a $pd delimiter
    $here = $ENV{'PWD'};
    # something to record the user
    $whoami = $ENV{'USER'} || $ENV{'USERNAME'} || (getpwuid($<))[0] || "Nobody";

    # handy to have these around for cross-platform work (different ascii line terminators)
    $EOL{'unix'} = "\012";              # \n
    $EOL{'mac'}  = "\015";              # \r
    $EOL{'pc'} = "\015\012";            # \r\n
}

With the above, one can code things like the following:

sub basename {
    local($string) = @_;
    $string =~ s|.*$pd([^$pd]*$)|$1|;
    return("$string");
}
sub notbasename {
    local($string) = @_;
    $string =~ s|(.*)$pd[^$pd]*$|$1|;
    return("$string");
}