SPUG:command line switches: the DWIMmyness that is Perl

Fred Morris m3047 at inwa.net
Mon Jan 20 01:59:24 CST 2003

Hi all,

I use Perl for all sorts of complicated things, but I confess to never
having played with the command line switches until tonight. I wanted
something which would expand tabs into spaces, but I wanted to be able to
specify the tab stop: sometimes it's 4, sometimes it's 8... anybody else
find themselves in the same predicament?

So I tried a BEGIN block, and it worked! That's cool!


Fred Morris
m3047 at inwa.net


#!/usr/bin/perl -wpi.orig


=head1 DeTab

Converts tabs to spaces, with a user-specified tab stop size.

=over 4

=item Author

Fred Morris

=item Creation Date


=item Copyright

(c) Copyright 2003 by Fred Morris, Seattle WA USA.
This utility can be used, distributed and modified
under the same terms as Perl itself.

The actual tab expansion comes out of one of the books on
the OReilly Perl CD.

=over 4

=item e-mail

m3047 at inwa.net

=item telephone


=item mailing address

6739 3rd NW
Seattle WA 98117 USA



=head2 Command Line Syntax

detab [-s<stop-size>] [<files>]

=head3 Arguments

=over 4

=item -s<stop-size>

Sets the size of a tab stop. The default tab stop size is 8.

=item <files>

A file glob, or a list of files to be edited in place. If none is
specified, input is read from STDIN and written to STDOUT.


=head2 Processing Details

Files, if specified, are "edited in place", with the original files
saved as the original files with an extension of .orig.


use vars qw/ $StopSize /;


    use Getopt::Std;

    my %Options;

    getopts( "hs:", \%Options );

    if (exists $Options{h}) {

        print "detab [-s<tab-size>] [<files>[...]]\n";

    $StopSize = ($Options{s}) ? $Options{s} : 8;


# The example on the CD uses a constant instead of $StopSize.

1 while s/\t+/' ' x (length($&) * $StopSize - length($`) % $StopSize)/e;

More information about the spug-list mailing list