ActiveState!

ActivePerl Documentation
Table of Contents

(Usage Statistics)
(about this ver)


* Getting Started
    * Welcome To ActivePerl
    * Release Notes
    * Readme
    * ActivePerl Change Log
* Install Notes
    * Linux
    * Solaris
    * Windows
* ActivePerl Components
    * Overview
    * PPM
    * Windows Specifics
       * OLE Browser
       * PerlScript
       * Perl for ISAPI
       * PerlEZ
* ActivePerl FAQ
    * Introduction
    * Availability & Install
    * Using PPM
    * Docs & Support
    * Windows Specifics
       * Perl for ISAPI
       * Windows 9X/NT/2000
       * Quirks
       * Web Server Config
       * Web programming
       * Programming
       * Modules & Samples
       * Embedding & Extending
       * Using OLE with Perl
* Windows Scripting
    * Active Server Pages
    * Windows Script Host
    * Windows Script Components

Core Perl Documentation


* perl
* perlfaq
* perltoc
* perlbook

* perlsyn
* perldata
* perlop
* perlreftut
* perldsc
* perllol

* perllexwarn
* perldebug

* perlrun
* perlfunc
* perlopentut
* perlvar
* perlsub
* perlmod
* perlpod

* perlstyle
* perlmodlib
* perlmodinstall
* perltrap
* perlport
* perlsec

* perlref
* perlre
* perlform
* perllocale
* perlunicode

* perlboot
* perltoot
* perltootc
* perlobj
* perlbot
* perltie

* perlipc
* perlnumber
* perlfork
* perlthrtut

* perldiag
* perlfaq1
* perlfaq2
* perlfaq3
* perlfaq4
* perlfaq5
* perlfaq6
* perlfaq7
* perlfaq8
* perlfaq9

* perlcompile

* perlembed
* perlxstut
* perlxs
* perlguts
* perlcall
* perlfilter
* perldbmfilter
* perlapi
* perlintern
* perlapio
* perltodo
* perlhack

* perlhist
* perldelta
* perl5005delta
* perl5004delta

* perlamiga
* perlcygwin
* perldos
* perlhpux
* perlmachten
* perlos2
* perlos390
* perlvms
* perlwin32

Pragmas


* attributes
* attrs
* autouse
* base
* blib
* bytes
* charnames
* constant
* diagnostics
* fields
* filetest
* integer
* less
* lib
* locale
* lwpcook
* open
* ops
* overload
* perllocal
* re
* sigtrap
* strict
* subs
* utf8
* vars
* warnings

Libraries


* ActivePerl
    * DocTools
        * TOC
            * RDF
* AnyDBM_File
* Archive
    * Tar
* AutoLoader
* AutoSplit
* B
    * Asmdata
    * Assembler
    * Bblock
    * Bytecode
    * C
    * CC
    * Debug
    * Deparse
    * Disassembler
    * Lint
    * Showlex
    * Stackobj
    * Terse
    * Xref
* Benchmark
* Bundle
    * LWP
* ByteLoader
* Carp
    * Heavy
* CGI
    * Apache
    * Carp
    * Cookie
    * Fast
    * Pretty
    * Push
    * Switch
* Class
    * Struct
* Compress
    * Zlib
* Config
* CPAN
    * FirstTime
    * Nox
* Cwd
* Data
    * Dumper
* DB
* Devel
    * DProf
    * Peek
    * SelfStubber
* Digest
    * HMAC
    * HMAC_MD5
    * HMAC_SHA1
    * MD2
    * MD5
    * SHA1
* DirHandle
* Dumpvalue
* DynaLoader
* English
* Env
* Errno
* Exporter
    * Heavy
* ExtUtils
    * Command
    * Embed
    * Install
    * Installed
    * Liblist
    * MakeMaker
    * Manifest
    * Miniperl
    * Mkbootstrap
    * Mksymlists
    * MM_Cygwin
    * MM_OS2
    * MM_Unix
    * MM_VMS
    * MM_Win32
    * Packlist
    * testlib
* Fatal
* Fcntl
* File
    * Basename
    * CheckTree
    * Compare
    * Copy
    * CounterFile
    * DosGlob
    * Find
    * Glob
    * Listing
    * Path
    * Spec
        * Functions
        * Mac
        * OS2
        * Unix
        * VMS
        * Win32
    * stat
* FileCache
* FileHandle
* FindBin
* Font
    * AFM
* Getopt
    * Long
    * Std
* HTML
    * AsSubs
    * Element
    * Entities
    * Filter
    * Form
    * FormatPS
    * Formatter
    * FormatText
    * HeadParser
    * LinkExtor
    * Parse
    * Parser
    * TokeParser
    * TreeBuilder
* HTTP
    * Cookies
    * Daemon
    * Date
    * Headers
        * Util
    * Message
    * Negotiate
    * Request
        * Common
    * Response
    * Status
* I18N
    * Collate
* IO
    * Dir
    * File
    * Handle
    * Pipe
    * Poll
    * Seekable
    * Select
    * Socket
        * INET
        * UNIX
* IPC
    * Msg
    * Open2
    * Open3
    * Semaphore
    * SysV
* LWP
    * Debug
    * MediaTypes
    * MemberMixin
    * Protocol
    * RobotUA
    * Simple
    * UserAgent
* Math
    * BigFloat
    * BigInt
    * Complex
    * Trig
* MD5
* MIME
    * Base64
    * QuotedPrint
* NDBM_File
* Net
    * Cmd
    * Config
    * Domain
    * DummyInetd
    * FTP
    * hostent
    * libnetFAQ
    * netent
    * Netrc
    * NNTP
    * PH
    * Ping
    * POP3
    * protoent
    * servent
    * SMTP
    * SNPP
    * Time
* O
* ODBM_File
* Opcode
* Pod
    * Checker
    * Find
    * Html
    * InputObjects
    * Man
    * Parser
    * ParseUtils
    * Plainer
    * Select
    * Text
        * Color
        * Termcap
    * Usage
* POSIX
* PPM
    * SOAPClient
    * SOAPServer
* Safe
* SDBM_File
* Search
    * Dict
* SelectSaver
* SelfLoader
* SHA
* Shell
* SOAP
    * Defs
    * Envelope
    * EnvelopeMaker
    * GenericHashSerializer
    * GenericInputStream
    * GenericScalarSerializer
    * Lite
    * OutputStream
    * Packager
    * Parser
    * Transport
        * HTTP
            * Apache
            * CGI
            * Client
            * Server
        * LOCAL
        * MAILTO
        * POP3
        * TCP
    * TypeMapper
* Socket
* Symbol
* Sys
    * Hostname
    * Syslog
* Term
    * ANSIColor
    * Cap
    * Complete
    * ReadLine
* Test
    * Harness
* Text
    * Abbrev
    * ParseWords
    * Soundex
    * Tabs
    * Wrap
* Thread
    * Queue
    * Semaphore
    * Signal
    * Specific
* Tie
    * Array
    * Handle
    * Hash
    * RefHash
    * Scalar
    * SubstrHash
* Time
    * gmtime
    * Local
    * localtime
    * tm
* UDDI
    * Lite
* UNIVERSAL
* URI
    * data
    * Escape
    * file
    * Heuristic
    * ldap
    * URL
    * WithBase
* User
    * grent
    * pwent
* Win32
    * AuthenticateUser
    * ChangeNotify
    * Clipboard
    * Console
    * Event
    * EventLog
    * File
    * FileSecurity
    * Internet
    * IPC
    * Mutex
    * NetAdmin
    * NetResource
    * ODBC
    * OLE
        * Const
        * Enum
        * NEWS
        * NLS
        * TPJ
        * Variant
    * PerfLib
    * Pipe
    * Process
    * Registry
    * Semaphore
    * Service
    * Sound
    * TieRegistry
* Win32API
    * File
    * Net
    * Registry
* WWW
    * RobotRules
        * AnyDBM_File
* XML
    * Element
    * Parser
        * Expat
    * PPD
    * PPMConfig
    * ValidatingElement
* XSLoader

 Getopt::Mixed - getopt processing with both long and short options


NAME

Getopt::Mixed - getopt processing with both long and short options


SUPPORTED PLATFORMS

  • Linux
  • Solaris
  • Windows
This module is not included with the standard ActivePerl distribution. It is available as a separate download using PPM.

SYNOPSIS

    use Getopt::Mixed;
    Getopt::Mixed::getOptions(...option-descriptions...);
    ...examine $opt_* variables...

or

    use Getopt::Mixed "nextOption";
    Getopt::Mixed::init(...option-descriptions...);
    while (($option, $value) = nextOption()) {
        ...process option...
    }
    Getopt::Mixed::cleanup();


DESCRIPTION

This package is my response to the standard modules Getopt::Std and Getopt::Long. Std doesn't support long options, and Long doesn't support short options. I wanted both, since long options are easier to remember and short options are faster to type.

This package is intended to be the ``Getopt-to-end-all-Getop's''. It combines (I hope) flexibility and simplicity. It supports both short options (introduced by -) and long options (introduced by --). Short options which do not take an argument can be grouped together. Short options which do take an argument must be the last option in their group, because everything following the option will be considered to be its argument.

There are two methods for using Getopt::Mixed: the simple method and the flexible method. Both methods use the same format for option descriptions.

Option Descriptions

The option-description arguments required by init and getOptions are strings composed of individual option descriptions. Several option descriptions can appear in the same string if they are separated by whitespace.

Each description consists of the option name and an optional trailing argument specifier. Option names may consist of any characters but whitespace, =, :, and >.

Values for argument specifiers are:

  <none>   option does not take an argument
  =s :s    option takes a mandatory (=) or optional (:) string argument
  =i :i    option takes a mandatory (=) or optional (:) integer argument
  =f :f    option takes a mandatory (=) or optional (:) real number argument
  >new     option is a synonym for option `new'

The > specifier is not really an argument specifier. It defines an option as being a synonym for another option. For example, ``a=i apples>a'' would define -a as an option that requires an integer argument and --apples as a synonym for -a. Only one level of synonyms is supported, and the root option must be listed first. For example, ``apples>a a=i'' and ``a=i apples>a oranges>apples'' are illegal; use ``a=i apples>a oranges>a'' if that's what you want.

For example, in the option description: ``a b=i c:s apple baker>b charlie:s'' -a and --apple do not take arguments -b takes a mandatory integer argument --baker is a synonym for -b -c and --charlie take an optional string argument

If the first argument to init or getOptions is entirely non-alphanumeric characters with no whitespace, it represents the characters which can begin options.

User Interface

From the user's perspective, short options are introduced by a dash (-) and long options are introduced by a double dash (--). Short options may be combined (``-a -b'' can be written ``-ab''), but an option that takes an argument must be the last one in its group, because anything following it is considered part of the argument. A double dash by itself marks the end of the options; all arguments following it are treated as normal arguments, not options. A single dash by itself is treated as a normal argument, not an option.

Long options may be abbreviated. An option --all-the-time could be abbreviated --all, --a--tim, or even --a. Note that --time would not work; the abbreviation must start at the beginning of the option name. If an abbreviation is ambiguous, an error message will be printed.

In the following examples, -i and --int take integer arguments, -f and --float take floating point arguments, and -s and --string take string arguments. All other options do not take an argument.

  -i24            -f24.5               -sHello
  -i=24 --int=-27 -f=24.5 --float=0.27 -s=Hello --string=Hello

If the argument is required, it can also be separated by whitespace:

  -i 24 --int -27 -f 24.5 --float 0.27 -s Hello --string Hello

Note that if the option is followed by =, whatever follows the = is the argument, even if it's the null string. In the example

  -i= 24 -f= 24.5 -s= Hello

-i and -f will cause an error, because the null string is not a number, but -s is perfectly legal; its argument is the null string, not ``Hello''.

Remember that optional arguments cannot be separated from the option by whitespace.

The Simple Method

The simple method is

    use Getopt::Mixed;
    Getopt::Mixed::getOptions(...option-descriptions...);

You then examine the $opt_* variables to find out what options were specified and the @ARGV array to see what arguments are left.

If -a is an option that doesn't take an argument, then $opt_a will be set to 1 if the option is present, or left undefined if the option is not present.

If -b is an option that takes an argument, then $opt_b will be set to the value of the argument if the option is present, or left undefined if the option is not present. If the argument is optional but not supplied, $opt_b will be set to the null string.

Note that even if you specify that an option requires a string argument, you can still get the null string (if the user specifically enters it). If the option requires a numeric argument, you will never get the null string (because it isn't a number).

When converting the option name to a Perl identifier, any non-word characters in the name will be converted to underscores (_).

If the same option occurs more than once, only the last occurrence will be recorded. If that's not acceptable, you'll have to use the flexible method instead.

The Flexible Method

The flexible method is

    use Getopt::Mixed "nextOption";
    Getopt::Mixed::init(...option-descriptions...);
    while (($option, $value, $pretty) = nextOption()) {
        ...process option...
    }
    Getopt::Mixed::cleanup();

This lets you process arguments one at a time. You can then handle repeated options any way you want to. It also lets you see option names with non-alphanumeric characters without any translation. This is also the only method that lets you find out what order the options and other arguments were in.

First, you call Getopt::Mixed::init with the option descriptions. Then, you keep calling nextOption until it returns an empty list. Finally, you call Getopt::Mixed::cleanup when you're done. The remaining (non-option) arguments will be found in @ARGV.

Each call to nextOption returns a list of the next option, its value, and the option as the user typed it. The value will be undefined if the option does not take an argument. The option is stripped of its starter (e.g., you get ``a'' and ``foo'', not ``-a'' or ``--foo''). If you want to print an error message, use the third element, which does include the option starter.


OTHER FUNCTIONS

Getopt::Mixed provides one other function you can use. abortMsg prints its arguments on STDERR, plus your program's name and a newline. It then exits with status 1. For example, if foo.pl calls abortMsg like this:

  Getopt::Mixed::abortMsg("Error");

The output will be:

  foo.pl: Error


CUSTOMIZATION

There are several customization variables you can set. All of these variables should be set after calling Getopt::Mixed::init and before calling nextOption.

If you set any of these variables, you must check the version number first. The easiest way to do this is like this:

    use Getopt::Mixed 1.006;

If you are using the simple method, and you want to set these variables, you'll need to call init before calling getOptions, like this:

    use Getopt::Mixed 1.006;
    Getopt::Mixed::init(...option-descriptions...);
    ...set configuration variables...
    Getopt::Mixed::getOptions();      # IMPORTANT: no parameters
$order
$order can be set to $REQUIRE_ORDER, $PERMUTE, or $RETURN_IN_ORDER. The default is $REQUIRE_ORDER if the environment variable POSIXLY_CORRECT has been set, $PERMUTE otherwise.

$REQUIRE_ORDER means that no options can follow the first argument which isn't an option.

$PERMUTE means that all options are treated as if they preceded all other arguments.

$RETURN_IN_ORDER means that all arguments maintain their ordering. When nextOption is called, and the next argument is not an option, it returns the null string as the option and the argument as the value. nextOption never returns the null list until all the arguments have been processed.

$ignoreCase
Ignore case when matching options. Default is 1 unless the option descriptions contain an upper-case letter.

$optionStart
A string of characters that can start options. Default is ``-''.

$badOption
A reference to a function that is called when an unrecognized option is encountered. The function receives three arguments. $_[0] is the position in @ARGV where the option came from. $_[1] is the option as the user typed it (including the option start character). $_[2] is either undef or a string describing the reason the option was not recognized (Currently, the only possible value is 'ambiguous', for a long option with several possible matches). The option has already been removed from @ARGV. To put it back, you can say:
    splice(@ARGV,$_[0],0,$_[1]);

The function can do anything you want to @ARGV. It should return whatever you want nextOption to return.

The default is a function that prints an error message and exits the program.

$checkArg
A reference to a function that is called to make sure the argument type is correct. The function receives four arguments. $_[0] is the position in @ARGV where the option came from. $_[1] is the text following the option, or undefined if there was no text following the option. $_[2] is the name of the option as the user typed it (including the option start character), suitable for error messages. $_[3] is the argument type specifier.

The function can do anything you want to @ARGV. It should return the value for this option.

The default is a function that prints an error message and exits the program if the argument is not the right type for the option. You can also adjust the behavior of the default function by changing $intRegexp or $floatRegexp.

$intRegexp
A regular expression that matches an integer. Default is '^[-+]?\d+$', which matches a string of digits preceded by an optional sign. Unlike the other configuration variables, this cannot be changed after nextOption is called, because the pattern is compiled only once.

$floatRegexp
A regular expression that matches a floating point number. Default is '^[-+]?(\d*\.?\d+|\d+\.)$', which matches the following formats: ``123'', ``123.'', ``123.45'', and ``.123'' (plus an optional sign). It does not match exponential notation. Unlike the other configuration variables, this cannot be changed after nextOption is called, because the pattern is compiled only once.

$typeChars
A string of the characters which are legal argument types. The default is 'sif', for String, Integer, and Floating point arguments. The string should consist only of letters. Upper case letters are discouraged, since this will hamper the case-folding of options. If you change this, you should set $checkType to a function that will check arguments of your new type. Unlike the other configuration variables, this must be set before calling init(), and cannot be changed afterwards.

$checkType
If you add new types to $typeChars, you should set this to a function which will check arguments of the new types.


BUGS

  • This document should be expanded.

  • A long option must be at least two characters long. Sorry.

  • The ! argument specifier of Getopt::Long is not supported, but you could have options --foo and --nofoo and then do something like:
        $opt_foo = 0 if $opt_nofoo;

  • The @ argument specifier of Getopt::Long is not supported. If you want your values pushed into an array, you'll have to use nextOption and do it yourself.


LICENSE

Getopt::Mixed is distributed under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

This means it is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details.

Since Perl scripts are only compiled at runtime, and simply calling Getopt::Mixed does not bring your program under the GPL, the only real restriction is that you can't use Getopt::Mixed in an binary-only distribution produced with dump (unless you also provide source code).


AUTHOR

Christopher J. Madsen <ac608@yfn.ysu.edu>

Thanks are also due to Andreas Koenig for helping Getopt::Mixed conform to the standards for Perl modules and for answering a bunch of questions. Any remaining deficiencies are my fault.

 Getopt::Mixed - getopt processing with both long and short options