[Israel.pm] Cool usage hack which I saw in another mailing list

Gaal Yahas gaal at forum2.org
Fri Mar 4 12:53:51 PST 2005

On Fri, Mar 04, 2005 at 05:02:50PM +0200, Omer Zak wrote:
>   open (SELF, $Me);
> The preceding is a really great idea. Why didn't everyone think of that?
> Much cleaner than an ugly usage() routine.
> Could you have found the location it's running from in order that it
> would keep on working even if you changed the scripts location?

As Srikanth mentioned, you can use $0 to get the filename, though
allegedly this does not work on all platforms. You could also use the
DATA filehandle (see perldata). I do give an example with usage() because
I like to return an error to the shell if incorrect usage was supplied:

     sub usage {
         my($level) = @_;
         print while (<DATA>);
         exit $level;
     usage: $0....

And, of course, someone did already think of that. Take a look at
Pod::Usage, or for a more radical approach, Getopt::Declare, where
apart from other features, the annotated option specification *is*
the usage information.

In Perl6, code will have a programmatic interface to its pod

Gaal Yahas <gaal at forum2.org>

More information about the Perl mailing list