On Fri, Oct 25, 2002 at 07:34:11PM -0700, Caroline Allen wrote:

> Hi, my name is Caroline Allen, and I'm a fairly long-time C programmer, 
> predominantly in the field of visual effects (you know, like, hacking 
> for movies). I have recently started using Perl, and I think it's 
> _great_, though getting up to speed with it is a bit like when I started 
> learning Spanish long after being fluent in French -- Perl has so many 
> similarities to C  and <n>shell that I trip over the differences. Things 
> only get more confused because I'm also a Lisp programmer, and a lot of 
> the dynamic/lexical scoping issues are very familiar to me, not to 
> mention the automatic memory management. Oh gosh, then there's Basic and 
> its own way of string handling (I was doing a lot of work in VB). As one 
> of my clients, Daffy Duck, would say, "Mother!"

Welcome!  I would imagine that with your background learning Perl will
mostly be a matter of getting to grips with the syntax, idioms,
idiosycrasies and learning what's already available in the form of
modules both in the core and in CPAN.

> I would really like to be using "getopt," and I'm failing. I can't 
> submit examples of my code to this list, for facility security reasons, 
> but if anyone could post some sample code using "getopt", or direct me 
> _to_ some sample code, that would be great. I browsed CPAN but didn't 
> find anything to the purpose, and I get the feeling that The Camel book 
> doesn't really address this very much, at least as far as I can find. I 
> would like to be using "getopt" because it models the behavior my client 
> base is accustomed to, i.e.  switch-based, non-positional arguments on 
> the UNIX command line.

I notice you've already got a couple of examples.  I'll also post the
template I use for new command line programs.  It is a little different
from the other two, but this is Perl, you take your pick.  You can run
the program and see what the -help, -info and -version options do.  The
other options are just examples.  The main difference with my approach
is that I keep all the options in a hash, along with other configuration
data.  I find this helps to reduce the number of global variables flying
around, and generally keeps things neater.

> I can't help wondering: does the Perl community have the same distaste 
> for non-positional arguments as the Lisp community seems to have?

I don't think so.  If anything, Perl 6 will provide greater support for
them.

> Working on a Friday night, waiting for the hellish traffic to subside,

I just cycled past it all.  Not that the traffic in Zürich is all that
bad anyway.


#!/usr/local/bin/perl

# Copyright 2002, Paul Johnson ([EMAIL PROTECTED])

# This software is free.  It is licensed under the same terms as Perl itself.

require 5.6.1;

use strict;
use warnings;

our $VERSION = "0.01";

# use My::Module;

use Getopt::Long;
use Pod::Usage;

my $Options =
{
    # add defaults here - eg
    file    => [],
    report  => "",
    summary => 1,
};

sub get_options
{
    die "Bad option" unless
        GetOptions($Options,  # store the options in the Options hash

                   # add options here
                   # probably always want help, info and version

                   "write:s" => sub
                   {
                       @$Options{qw(write summary)} = ($_[1], 0)
                   },
                   qw(
                       help|h!
                       file=s
                       info|i!
                       report=s
                       summary!
                       version|v!
                     ));
}

sub main
{
    get_options;

    # check validity of options

    print "$0 version $VERSION\n" and exit 0 if $Options->{version};
    pod2usage(-exitval => 0, -verbose => 0)  if $Options->{help};
    pod2usage(-exitval => 0, -verbose => 2)  if $Options->{info};

    # handle whatever is left in @ARGV

    # main program
}

main

__END__

=head1 NAME

program - description

=head1 SYNOPSIS

 program -help -info -version
         -summary -report report_format
         -file filename
         other options

=head1 DESCRIPTION

What it does.

=head1 OPTIONS

The following command line options are supported:

 -summary              - give summary report          (default on)
 -report report_format - report format required       (default none)

 -file filename        - only report on the file      (default all)

 -h -help              - show help
 -i -info              - show documentation
 -v -version           - show version

=head1 DETAILS

What it does in more detail.

=head1 EXIT STATUS

The following exit values are returned:

0   All operaions were completed successfully.

>0  An error occurred.

=head1 SEE ALSO

 My::Module

=head1 BUGS

If you have put any in.

=head1 VERSION

Version 0.01 - 8th October 2002

=head1 LICENCE

Copyright 2002, Paul Johnson ([EMAIL PROTECTED])

This software is free.  It is licensed under the same terms as Perl itself.

=cut

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to