MacPerl used here
    

About the MacPerl FAQ

Under Construction

Version and Date information:

This is version 2.7b (in progress), last revised on Thursday, July 8, 1999.

Changes since prior release:

  • 11/11 - comment RE: Faq-o-matic
  • 7/30 - comments RE: Net::SMTP and 68000 support
  • 7/29 - comments from Paul Schinder:
    name of comp.lang.perl.moderated, ftp site for Alpha, 68000 systems, Perl 4 vs 5 information...
  • 7/28 - posted update
  • 7/26 - Updated from version 2.5 (March 1997) - many changes
This FAQ is planned to be installed into the FAQ-O-Matic Real Soon Now. Stay Tuned.
See below for locations of the current FAQ.

The MacPerl FAQ is currently being edited and maintained by Vicki Brown, Rich Morin, and other members of the MacPerl community at www.ptf.com/macperl/depts/fom.html.

The MacPerl FAQ-o-matic is being kept on the MacPerl Pages at http://www.cfcl.com/macperl/depts/fom.html. The FAQ-o-matic is maintained entirely by your contributions.

The initial version of the MacPerl FAQ was prepared by Hal Wine. The current outline and a lot of the useful words in this edition were prepared by Hal, as well. Later versions of the FAQ were edited and maintained by Bob Dalgleish and Bob Wilkinson.

This FAQ is intended as a supplement to the main Perl FAQ, and does not replace it.

Where to get the most recent MacPerl release:

The best way to get official releases of the MacPerl application or tool is to use the Comprehensive Perl Archive Network at www.perl.com/CPAN. A pointer to the latest version can also be found on The MacPerl Pages.

  • Use FTP to get the site registry and find the repository site nearest you. Save this information for later use when you want to get files. It saves wear and tear on the 'net and administrators everywhere.

  • Use a Web browser to search for the file to download. This URL will find a (relatively local) mirror site to get your file. This URL uses a magic process to refer you to a nearby FTP site. There is also a form that will identify the most recent versions of released files and give you the URL without the search.

Where to get the most recent FAQ:

The most recent copy of the FAQ is available at the MacPerl FAQ-O-Matic or from the CPAN at ...doc/FAQs/mac.

What the FAQ covers:

The MacPerl FAQ tries to answer simple questions that are unique to the various versions of MacPerl. For more complex questions, pointers to more detailed information may be given.

The MacPerl FAQ also contains information on some unique applications of MacPerl. However, pointers to other information are more likely for these topics.

Finally, the MacPerl FAQ contains information useful in porting Perl scripts to and from the Unix environment.

What the FAQ doesn't cover:

The MacPerl FAQ does not cover basic Perl questions. Nor does it cover basic Macintosh programming information. It certainly doesn't cover details of WWW CGI programming! This is handled slightly differently on the Mac than on other platforms, and pointers are provided for detailed information.

The MacPerl FAQ is also not a tutorial. Nor is it a 100% correct authority on anything! If you think something is missing, we welcome you to add it to the MacPerl FAQ-O-Matic

How the FAQ is organized:

This FAQ is organized from general information through more specific information. A given topic is covered in the most general location possible.

Both Perl version 4 and 5 have been ported to the Mac. Much of the information in this FAQ is not unique to the Perl version being used. The major emphasis of the FAQ is on MacPerl 5.


Table of Contents


1) General Questions

1.1) What's the most recent version of MacPerl?

MacPerl is available in forms corresponding to Unix Perl versions 4 and 5. Both of these are also available as either a standalone Macintosh application, or as a tool for the MPW (Macintosh Programmer's Workshop) environment.

Unless you already have MPW, and know its environment, you want the standalone version, hereafter called the MacPerl Application.

Perl5 introduced several powerful new technologies, including object-oriented programming, to Perl. This allowed modules to be much more self-sufficient, yet extensible, as well as allowing system extensions. These extensions will be capitalized upon in further releases of MacPerl.

Where possible, the users of MacPerl should upgrade to MacPerl5, since its extensibility and generality are very much enhanced. Perl4 scripts should run almost unchanged under Perl5, but you can easily check for problems by setting the -wswitch to flag common errors. The reference file pod:perltrap.html in the release describes common problems.

1.1.1) Perl 5

The latest version of MacPerl is available from the CPAN archives in directoryhttp://perl.com/CPAN/ports/mac/ as files:
Mac_Perl_tool.bin
for the MacPerl tool
Mac_Perl_appl.bin
for the MacPerl application
Both of these files are in MacBinary format. Other versions may also be available in those directories. For MacPerl, the version naming convention also refers to the Unix Perl version. For example, MacPerl version 5.2.0r4 can be deciphered as follows:5 is the major Perl release, .2.0 is the Mac revision level, andr4 is the patchlevel of the Perl core.

The source code for these versions is also available in the same directory. Consult the file Mac_Perl.info for details on which sources are needed for which version.

1.1.2) Perl 4

The last version of Perl 4 ported to the Mac is MacPerl 4.1.8, which corresponds to Unix Perl version 4.036 (the last release of Perl 4). While it is quite stable, and has a lot of users still, MacPerl4 is no longer being developed or supported. Many of the newer Perl modules will not work with Perl 4. The latest versions (4.1.8) are still available from the CPAN archives in directory ports/mac/ as file:
Mac_Perl_tool.bin
for the MacPerl tool
Mac_Perl_appl.bin
for the MacPerl application
Both of these files are in MacBinary format.

1.1.3) Differences from Perl 4 to Perl 5

There hare many differences between Perl 4 and Perl 5. For the most part, however, scripts written for Perl 4 will run unaltered under Perl 5.

One noticeable difference is how package qualifications are made. In Perl4, a variable in a package was entered as $Package'variable. In Perl5, it is entered as $Package::variable, although the old form is still permitted.

Other differences include:

  • some old keywords are gone
  • some new keywords have been added
  • new quoting constructs have been added
  • Perl 5 has object oriented features
  • Perl 5 has modules
  • myis recommended instead of local
  • more diagnostics are possible
  • certain coding habits that should not have been permitted now generate appropriate complaints ;-)

If you have been using Perl 4 and are moving to Perl 5, consult the Perl 4 to Perl 5 Traps list or perltrap.pod in the release help files, for guidance. If you are just now learning Perl, be sure to start with the latest version. Be sure that any books you refer to document Perl version 5!

1.2) How do I install MacPerl?

No matter which version of MacPerl you need, the installation process is quite similar.

1.2.1) Transferring the files:

Be sure to transfer the files in binary mode.

After decompressing the archive, be sure to read all the text files in the top leveldirectory. (Or just review them if you're upgrading.)

1.2.2) How do I unpack the stuff I got?

In order to enable transmission of Macintosh files across the internet, they must be encoded in various formats. Each layer of encoding adds a suffix to the file.

All MacPerl distribution files are StuffIt formatted archives, encapsulated in MacBinary. Use StuffIt Expander, available from Aladdin Systems, or a similar utility, to decompress the files. See the comp.sys.mac.comm FAQ for a more complete list

1.2.3) Setting Preferences

Preferences are set for the MacPerl tool by pulling down the "Edit" menu, and selecting "Preferences". This permits the user to

  • Choose the path along which libraries are searched

  • Determine whether MacPerl defaults to opening files for editing, or tries to run them. The decision whether to check for a line starting with the string "#!" can be toggled.

  • The decision whether to check for inline input can be toggled

  • "Internet config" can be launched, which enables various parameters to be set

Preferences for the MPW tool are set as described in section 4.1.1.

1.3) What Macs does MacPerl run on?

MacPerl runs on any Macintosh, including both m68K-based machines and Power Macintosh models. MacPerl version 5 (and version 4) runs on the 68020, 68030, and 68040 (Quadra) systems as well as all Power Macintosh models. However, only MacPerl version 4 will run on the actual 68000 chipset (i.e. the MacPlus). You'll need at least 4 MB of RAM and System 7 or above to run MacPerl version 5 or later.

Also, to run MacPerl 5 on a 68K machine, you'll need to install a special version of the appliation. The alternatives are the "Big" application and the"CFM68K" application. "CFM68K" should be supported on all 68030 or 68040 Macintosh models running System 7.6.1 or later. If you have a 68020 (Mac II or SE) or are using earlier System Software (e.g. 7.1 or 7.5), install the "Big" application.

1.4) Other MacPerl resources:

Take a look at The MacPerl pages for pointers to many resources for MacPerl, including the MacPerl primer, books, mailinglist archives, and many other resources.

1.4.1) Is there a mailing list for MacPerl?

Yes! To join, send email to mac-perl-request@iis.ee.ethz.ch. The BODY of the message (the Subject is ignored) is the word "subscribe" (or"unsubscribe"). Please do NOT send your (un)subscription notice to mac-perl; you will merely annoy people :-).

Or use the web-based subscription form on The MacPerl pages

A searchable archive of the mailing list is available.

The mailing list is presently unavailable in digest form.

1.4.2) Newsgroups which cover Perl (not specifically MacPerl)

1.4.3) Are there any books on MacPerl?

Currently, there is one available.
MacPerl: Power and Ease
by Vicki Brown and Chris Nandor
published May, 1998, by Prime Time Freeware
ISBN 1-881957-32-2
More information is available on The MacPerl Pages.

1.5) Perl Resources

1.5.1 What are the recommended books about Perl?

The canonical Perl book is
Programming Perl
by Larry Wall, Tom Christiansen, & Randal L. Schwartz
Second Edition
published Sept., 1996, by O'Reilly & Associates
ISBN 1-56592-149-6
This book is affectionately known as "The Camel book" and was co-authored by Perl's creator, Larry Wall, and two other well-known Perl experts. No one in the Perl community will question your judgement in buying this book. It's a must-have for any Perl programmer's bookshelf.

Note - not all examples in the typical Perl books will run unmodified under MacPerl! Most Perl books were written for Unix Perl (or perhaps Perl for Windows NT). See the section on differences for more details.

1.5.2 Where do I look for more Perl books and documentation?

The main page for Perl documentation is at perl.com. Perl books and magazines are specifically listed on their own page.

You can also search the online bookstores. Try Computer Literacy or Amazon.com.

1.5.3 Are there any book reviews online?

Tom Christiansen publishes an on-line review of books about Perl on perl.com under Camel Critiques.

Amazon.com asks readers to post reviews of books they have purchased and read.

2) Differences between MacPerl and Perl on other platforms

2.1) Differences in functions on MacPerl, compared with Perl under Unix.

Routine MacPerl 4 MacPerl 5
alarm Not implemented
exit Needs to be accompanied by MacPerl::Quit to do what you expect.
fcntl Meaning of controls is system specific Meaning of controls is system specific.
POSIX.pm provides a useful interface.
fork Not implemented
getlogin
getpgrp
setpgrp
getppid
getpw*
getgr*
setgr*
endgrent
endpwent
Not implemented
ioctl Meaning of controls is system specific Meaning of controls is system specific.
POSIX.pm provides a useful interface.
kill Not implemented
link Not implemented
msg* Not implemented
chmod Meaning of the mode of files is different
chown Meaning of ownership is different
chroot Not implemented
crypt Not implemented Implemented but not distributed in the sources.
dump Not implemented. See runtimes.
exec Not implemented
open Pipes are not supported, except withToolServer support.
pipe Not implemented, except with ToolServersupport.
shm* Not implemented
syscall Not implemented
time Uses January 1, 1904 as base (Unix uses January 1, 1970)
utime Not 100% compatible
umask * Does nothing
wait Not implemented
waitpid Not implemented

2.2) Compatability with Perl under Unix.

2.2.1) Why does source preprocessing (-P) not work?

Language processors on Unix have traditionally allowed arbitrary front end preprocessing for their input. The Mac has not.

Perl5 has significantly extended the syntax and the preparation so that most preprocessing is not needed. The usual use of preprocessing is to provide manifest constants. Subroutines can be made into "bare-words", which allow what-look-to-be constant values, likeENOFILE, to be used in Perl code.

2.2.2) How do I open a pipe both to and from a command?

You don't on the Mac.

2.2.3) Why do %ENV changes not pass to processes that I start?

The environment variables are aspects of Unix and MSDOS command processing. They are supported only within single processes on the Mac for compatibility purposes.

2.2.4) Why don't backticks work as they do on Unix?

The Mac OS doesn't (really) support the notion of subprocesses. These can be faked from MacPerl and MPW if you have an application called "Tool Server". Tool Server is sold as part of various Mac development tools (such as Symantec C and Metroworks). It is also available from Apple's MPW ToolZone. Even so, there are many limitations that are not present under Unix.

Matthias has hardcoded some strings to be processed by MacPerl itself.

pwd
Current working directory.
Directory
Current working directory.
hostname
Hostname as returned by MacTCP or OpenTransport.
stty -raw
Allow single character input from the keyboard.
stty -cooked
Allow normal character input (line-oriented) from the keyboard.
See the documentation file MacPerl.Specifics in your release for up-to-date information. (This file has been superceded in MacPerl5 by the pod:macperl.html file).

For greater portability, there is a module called Cwd.pm which sets the environment variable PWD. For instance,

    use Cwd;
	    chdir_init;
	    $p = $ENV{'PWD'};
	    ...
	    chdir $newdir;
	    $p = $ENV{'PWD'};
	

2.3) MacOS file system issues

2.3.1) How do I request an input or output file under MacPerl?

       use Mac::StandardFile;
       $file = StandardGetFile('', 'TEXT');
       if ($file->sfGood()) {
           push(@ARGV, $file->sfFile());
       } else {
           exit(1);
       }
 	
	
The module, Mac::StandardFile. is supplied in the lib folder. It presents a convenient front-end to the more primitive routine, MacPerl::Choose, which is discussed in pod:macperl.html.

2.3.1.1) How do I request a folder under MacPerl?

require 'GUSI.ph';
	
	$chosen_folder = MacPerl::Choose(&GUSI::AF_FILE(), 0, "Which folder?",
                     "", &GUSI::CHOOSE_DIR);

	
The file, GUSI.ph. is supplied in the lib folder.

2.3.2) Why can't I detect aliases in my script?

Droplets *never* see aliases in @ARGV, as the finder resolves them before Perl sees them.

The same is true for the results of standard file calls.

This has been fixed in MacPerl5r2.

2.3.3) How do I pass environment variables to my script?

The "environment" parameter must be passed as a list of strings (name/value pairs). Here is a snippet that works:
	property env : {"HOME", "Boot", "TEMP", "Boot:Temporary Items"}
	property perlScript : "Boot:Scripting:MacPerl:called_by_as_test.pl"
	property opts : {"-n", "-x", "Boot:Scripting"}
	
	tell application "MacPerl"
	   try
	      Do Script ({perlScript} & opts) mode batch environment env
	      without extract
	   on error errorMessage number errorNumber
	      return errorNumber
	   end try
	end tell
	

2.3.4) How do I perform globbing in MacPerl?

For MacPerl4, the functionality of globbing can often be achieved using the opendir, readdir, closedir commands. For example,
        @files = <*.error>;
	
is equivalent to:
    opendir( DIR, ':' ) || die( "Can't open ':': $!\n" );
	    @files = grep( /\.error$/, readdir( DIR ) );
	    closedir( DIR );
	
For MacPerl5, globbing is available.

2.4) Application additions

2.4.1) MacPerl package

MacPerl comes with a built-in package of routines and variables which are accessed using the syntax $MacPerl::Variable and &MacPerl::Subroutine. These are documented in the file pod:macperl.html.

2.4.2) Macintosh Toolbox interfaces

The Macintosh Toolbox interfaces are being prepared and tested. To find out about these interfaces, you had best join the MacPerl mailing list. See mailinglist section for details.

2.5) How can I compare two date strings?

See the Perl FAQ.

2.6) How do I get MacPerl to recognize the Mac character set?

A byte can store 8 bits, however, there is only a general agreement as how to use the bottom 7 bits (i.e. the first 128) for representing characters. This standard is ASCII. (Of course, even this has detractors e.g. IBM's use of EBCDIC).

The top 128 characters which can be stored in a byte are the extended characters. However, which character is represented by which bit-pattern is font-dependent, and especially machine-dependent. Some machines do share the same extended character set e.g. Unix and MS-Windows both use the ISO-lat1 (Latin-1) character set; this is not the same as that generally used on a Macintosh.

In regular expressions, \w can be used to match against an alphanumeric character, where this set of characters is [A-Za-z0-9_]. This will not match against å, é, ü, è or any other accented characters which are found in the extended characters of the Macintosh character set (or any other extended characters for this matter). However, locale settings can influence the characters matched by \w.

Portability of code (from platform to platform, and on the Macintosh from font to font) would be compromised, if the extended characters were used within the \w, \W matches. A set of characters may be constructed on an ad hoc basis by encoding the characters as their hex values e.g. \xa9.

The library function, I18N:Collate may be used to order objects according to the national character set used.

2.7) How do I access Mac Resources from MacPerl?

You don't, if you want portable code! If you do want to access them, you have to use an XCMD or XFCN (see Extending Perl for information on that).

Recently, Matthias has released Perl extensions that allow access to the Macintosh toolbox for Memory, Resources, and Apple Events. Currently, this is PowerMac-compatible only.

3) MacPerl Application

3.1) How do I package my Perl scripts?

First, let's look at what you have to distribute in order to share a perl script with a friend e.g. your wiz bang lotto number picker script.

Two methods of distributing clickable run-time applications are supported, Droplets and Runtimes.

3.1.1) Droplets:

Droplets are mini applications that work in conjunction with MacPerl (the application) under System 7. (Versions of MacOS prior to 7.5 require that AppleScript is installed, too; 7.5 added this to the OS implicitly). They are the "recommended" way to package perl scripts on the Mac.

To make a droplet, simply open your script in MacPerl, then choose "Save As". One of the "Type" options is "Droplet". The droplet is actually an application program that contains the text of your script, plus some code (about 8KB). When launched, the droplet uses Apple Events to launch MacPerl (if it isn't already running), and then sends it the script to execute.

You can reopen and change the script of droplets from within MacPerl. Subsequent "Saves" will retain the droplet file type.

If the user launches a droplet by dragging and dropping some files onto the droplet's icon in the Finder, those file names will appear as arguments to the script (i.e. they will be in @ARGV). This is one of the simplest ways to access the command line from outside of MPW.

The only downside to droplets is that users must have a complete installation of MacPerl, including any libraries that are called by your script. This means that the first application you distribute this way take over 800K. However, future droplets only take up 8K over the size of the script.

3.1.2) Standalone applications/Runtimes:

Saving your script as a runtime version (for either system 7 or systems 6 & 7) is another way to package a script as a double-clickable application. However, droplets are the preferred method.

If having your users install a version of MacPerl is too onerous (required for droplets), then you can save them as a runtime application. However, not all features are supported in the runtime applications. Also, runtime versions take over 500KB per script!

Note: runtime versions do not execute any quicker. Perl is always interpreted every time the script is run, period.

3.1.3) What about other files to go with the package?

The Perl way to do things is to have Perl modules (files ending in .pm) placed in the script's directory, or in the library, where they will be found by searching the @INC path. (See Section 1.2.3 for details of how this is set.)

The Macintosh way is to package everything that is needed into a single file using (named) resources to differentiate components. In support of this, MacPerl allows you to include CODE resources (of type XCMD or XFCN), as well as TEXT resources containing source modules, into the droplet or runtime. To get access to the CODE resource, you need to use theMacPerl::LoadExternals() function. To get access to the source modules, therequire function searches the TEXT resources of the droplet for the module name. (See Extending Perl .)

3.2) Devices (printer, screen, apple event)

3.2.1) How can I get MacPerl to dial my modem?

Look at CommConnect.t in the :t:mac folder. One note: currently it seems to be impossible to use the Apple Modem Tool successfully, so you have to rely on sending a line like
   ATDT 1-900-MACPERL
	
to the Serial Tool.

3.2.2) How can I keep local data files in my droplet/runtime?

You can access TEXT resources by name using open(STREAM, "Dev:Pseudo:Stream"); for some resource named Stream.

3.2.3) How can I output to another window?

open(CON2, ">Dev:Console:Another Window");
	

3.2.4) How can I get/set AppleEvent data?

Open Dev:AEVT:FOOB, data associated with event parameter FOOB is read/written.

3.3) How do I develop scripts?

3.3.1) Alternate Text Editors

The default MacPerl editor, based on Apple's TextEdit engine (used in TeachText and SimpleText) is limited in its capabilities. You may want to consider getting an alternative text editor.

The shareware editor, Alpha, and the commercial editor, BBEdit, (as well as the freeware version, BBEdit Lite), contain many useful features designed to make it easier to write (and run) Perl scripts (as well as many other programming languages). These features include line numbering, real tabs, the ability to edit files larger than 32K in size, language-sensitive syntax coloring, search/replace capability, subroutine indexing, and much more.

3.3.2) Alpha Text Editor:

Alpha is available in two forms, both shareware. Check the Alpha home page for more information. The FTP site has the most recent version, and it is available on all Mac repositories.

3.3.3) BBEdit Text Editor:

BBEdit is available in both commercial and free (Lite) versions. Be sure to use version 4.5 or later for complete Perl 5 syntax compatibility. If you use BBEdit, you'll also want Brad Hansen's BBEdit plug-ins.

4) MacPerl MPW tool

4.1) Installation of MPW MacPerl

The steps for installing the MPW tool have been greatly simplified. Be sure you have installed the complete MacPerl application (the .appl archive) first; the MPW tool depends on many of the files and folders installed with the application. Themn use the MacPerl MPW tool installer to install the tool; choose the same volume and folder where you previously installed the application.

To complete the installation proces, launch MPW and run the Install.MPW_Perl script (this was part of the tool package you just installed). Follow the instructions. This MPW script will create configuration file which you must edit and execute to complete the installation process.

4.2) Specific differences between the MPW tool and Perl under Unix

The functions which do not work in the same way are outlined in the differences table, but otherwise its usage is similar, subject to the differences between MPW and a Unix environment.

5) Special Application Areas

5.1) How can I extend the capabilities of MacPerl?

AppleScript scripts can be compiled and executed by MacPerl. With a suitable collection of AppleScript Scripting Additions, many system manipulation functions are quite straightforward to perform. With the presence of the Scriptable Finder, many file manipulation operations are quite easy.

The code modules (XCMD/XFCN) developed for HyperCard may often be used in MacPerl. While there is no external documentation for which HyperCard callbacks are not implemented,most widely available code module libraries offer safe packages. See themacperl.html file in the pod directory of MacPerl for how to useMacPerl::LoadExternal().

5.1.1) Perl Modules

See the Perl5 FAQ.

5.1.2) Perl Extension System (XS) and dynamic loading of extensions.

Perl 5 introduced the concept that the Perl platform was truly extensible by knowledgeable programmers. External code modules can be written and imported into the Perl application at run-time, while presenting a Perl-ish interface to other programs. This is often the only way to get at many operating systems functions.

Starting with MacPerl5r2, the extension system has gotten a lot easier, and Matthias has written a number of modules that give access to the MacOS functions. These extension modules are still in beta testing, but will be made available for general release.

5.2) How do I program for the Web?

5.2.1) How do I serve Web information (CGI)?

Learn the basics of the Common Gateway Interface.

Then, get the CGIglue

Next, get some documentation from Using Perl With MacHTTP and MacUser April 1997.

Other links which may be of use are MacWorld, Mac WWW: Network Questions, and MacHTTP web site.

5.2.2) How do I mail the contents of a filled in form?

Use the Net::SMTP module (distributed with MacPerl). Or, try out MailTools, Graham Barr's module to compose mail and manipulate Unix standard mailboxes. Check http://pobox.com/~schinder/MacPerl.html. for the most recent port of MailTools to MacPerl.

5.2.3) How do I create or parse HTML documents?

The best package for this set of tasks is libwww. The libwww module is included in the MacPerl distribution as of version 5.2.0r4. A more recent port may be available at http://pobox.com/~schinder/MacPerl.html, courtesy of Paul Schinder. (The ports for MacPerl version 4 are no longer available at this site).

If you want to do smaller versions of these tasks, then try Tom Christiansen's hacks for useful scripts.

5.3) Portability tricks and techniques

5.3.1) How do I know that this script is running under MacPerl?

The basic method of determining whether the script is running under MacPerl or some other Perl is the test.
    print "Running under MacPerl\n" if $MacPerl::Version;
	
Under MacPerl5, there is a more portable method, sanctioned by the Perl development community:
    use Config;
	    if ( $Config{'osname'} =~ /^macos/i ) {
	        print "Running under MacPerl\n";
	    }
	 

Accessing file names is different between Unix and MacOS. However, Unix, MacOS and MSDOS all use hierarchical file naming, and some of the commonality has been gathered together into Perl modules for Perl5. Learn to use the portability-oriented modules, such as File::Basename.pm, and File::Path.pm.

5.3.2) How do I get input from the user of the script?

The simplest method is to use the MacPerl::Ask() and MacPerl::Answer() methods.

5.3.3) How do I pass parameters to the script?

The first line of the Perl script has room for one argument. For instance,
#!perl -pi.BAK
	
will invoke MacPerl with the parameters -p (for wrapping an input loop around the code with autoprint), and -i.BAK (for renaming the input file with .BAK appended, and rewriting the file).

In the MPW version, the command line is supplied in the same manner as on Unix. For the application, there are two choices, depending on whether or not you need just file names or also options.

If you also want to give the user a chance to enter command line options, you can use the following code fragment:

   if( $MacPerl::Version =~ /Application$/ ) {
	        # we're running from the app
	        my( $cmdLine, @args );
	        $cmdLine = &MacPerl::Ask( "Enter command line options:" );
	        require "shellwords.pl";
	        @args = &shellwords( $cmdLine );
	        unshift( @ARGV, @args );
	    }
	

If your script just needs to know file names, then you can save the script as a droplet and start it by dropping the desired files onto the droplet's icon from the Finder.

If the parameters you need to supply are mostly fixed, you can also put them after the script itself, in the data portion of the script. For instance,

	# Get the default parameters
	 {
	   my @parms = <DATA>;
	    chomp(@parms);
	    unshift(@ARGV, @parms);
	}
	...
	 __END__
	parm1
	parm2
	...
	

5.4) How do I know what directory the current script is in?

$0 contains the full path to the script that is executing.
    $dirSep = '/';  # assume Unix
	    $dirSep = ':' if $MacPerl::Version;  # change to Mac
	    ($path, $app) = $0 =~ /^(.*$dirSep)?([^$dirSep]+)$/;
	
or, better because it uses standard facilities,
    use File::Basename;
	    ($app, $path) = fileparse($0);
	

6) Common Problems

6.1) Why does my here document not get read properly?

  1. there isn't a carriage return after the final HEREIS or
  2. there is a bunch of white space after the final HEREIS
To put it in Perl terms, the line that terminates the string must be exactly the line that would be produced by the perl code:
	print "HEREIS\n";
	

6.2) Why does MacPerl not work with Netscape?

One release of MacPerl installed an incorrect version of ObjectSupportLib into the System Folder on PowerMacs. If ObjectSupportLib is installed, it should be version 1.2 or later.

Note that MacOS 8 does not require a separate ObjectSupportLib; this functionality is built into the System.

6.3) When I try to read a particular file, why does MacPerl complain that it is "out of memory"?

If the file that you are trying to read is big and was prepared on a Unix system, and not translated to the Mac environment (for instance, FTP transfer in binary mode), then each line in the file will have newline instead of carriage-return at the end. This causes MacPerl to try to read the entire file at once, in an inefficient manner.

Set the record separator character to "\012" using any of

	
  • $/ = "\012";
  • use English; $INPUT_RECORD_SEPARATOR = "\012";
  • use English; $RS = "\012";

Alternatively, translate the file using an editor or file conversion program. Or, convert it using Perl! For example:

	$/="\012";$\="\010";while(<>){chop;print}
	
or
	$/ = "\012";
	$^I = ".orig";

	while (<>) {
    	s/\012/\015/g;
    	print;
	}
         
(Remember, There's More Than One Way To Do It :-)

6.4) I can't get my droplet to run when I drop files on it?

Through some low level interactions, software used in the AppleScript system is needed to operate the droplet mechanism. Get the current version of AppleScript and install it in the Extensions Folder of your system.

Feedback about this site: Use our feedback form or send Email to: webmaster@ptf.com
Copyright © 1997 Prime Time Freeware
This page was last updated: 10:20 on Nov. 11, 1998.