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

[MacPerl] set a startup directory under MacPerl



I'm showing off a module here, in case anyone finds any of the code
in it useful.

I got tired of the startup directory for MacPerl droplets (or other
programs) not being anything terribly useful (at least as I use
them).
So I wrote the below-included module.
("SMB" is just my initials -- it's how I start the name of all
modules that I don't anticipate ever putting in CPAN.)

With this module, if you say "use SMB::Session" in a program run
under MacPerl, it chdirs to whatever directory the program is in (or
the desktop folder, if it's an Untitled window), and then, unless
it's a CGI, prints something like:

† «foono» start at Wed Sep 22 16:41:53 1999 in «Slig:MacPerl ź»
[all the program output here]
† «foono» runtime 1s, ending Wed Sep 22 16:41:54 1999

I even added some basic code so it does something comparable under
non-MacPerl Perls, minus the chdir stuff.


use strict;
package SMB::Session;
use vars qw($VERSION $Debug $HOME $OQ $CQ $PLUG);
$VERSION = 0.19;
$Debug = 0 unless defined $Debug;
use File::Basename;

if($MacPerl::Version) {
  $OQ = "\xC7"; $CQ = "\xC8"; $PLUG = "\xBA";
   # mac ascii for open/close chevrons, and integral sign
  if(index($0, ':') == -1) { # a program in an untitled window...
    eval
     'use Mac::Files; $HOME = FindFolder(kOnSystemDisk, kDesktopFolderType);'
    ;
  } else { # normal...
    $HOME = dirname($0);
  }
  die "unexpected error in eval: $@" if $@;
  unless($HOME eq ':') {
    chdir $HOME or die "Couldn't chdir to $HOME!";
  }
} else {
  $OQ = "\xAB"; $CQ = "\xBB"; $PLUG = "\xA7";
   # latin-1 for open/close chevrons, and section sign
}

unless(exists($ENV{'SCRIPT_NAME'}) && length($ENV{'SCRIPT_NAME'})) {
  my $bit = basename($0) || $0;
  eval <<"eothing"; # build a custom END block
    END {
      my \$now = time;
      print
        "$PLUG $OQ$bit$CQ runtime ",
        \$now - $^T,
        "s, ending ", scalar(localtime(\$now)), "\n\n";
    }
eothing

  print "$PLUG $OQ$bit$CQ start at ",
   scalar(localtime),
   $HOME ? " in $OQ$HOME$CQ" : '',
   "\n";
}

1;

__END__




===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org