Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Permutations, Getopt::Long, and Pod::Usage

by Ovid (Cardinal)
on Feb 05, 2003 at 21:08 UTC ( #232927=snippet: print w/ replies, xml ) Need Help??


This is a quick hack for a friend. It can be improved upon. Due to a recently discovered triple extension security hole in Outlook Express and the limitations of his email filters, he wanted to be able to generate a list of all possible permutations of extensions, limited to 3 extensions per item. The extensions are in a file, one extension per line. Number of items per permutation and optional outfile can be specified on the command line.

The permutation function was borrowed from merlyn's code and is reproduced here primarily to provide useful examples of the modules mentioned in the title.

#!/usr/bin/perl -wl
use strict;
use Getopt::Long;
use Pod::Usage;

my ($IN,$OUT,$COUNT);

    'help|?'  => sub { pod2usage(-verbose => 2);exit } ,
    'in=s'    => \$IN,
    'out=s'   => \$OUT,
    'count=i' => \$COUNT
die pod2usage {-verbose=>2} unless $IN;

open IN, "< $IN" or die "Cannot open ($IN) for reading: $!";
chomp (my @items = <IN>);
close IN;

$COUNT ||= scalar @items;

my @permutes;
push @permutes => \@items for 1 .. @items;
my %results = map { join( '',@$_[0..($COUNT-1)]) => 1 } permute(@permu

if ( $OUT ) {
    open OUT, "> $OUT" or die "Cannot open ($OUT) for writing: $!";
    print OUT $_ foreach sort keys %results;
    close OUT;
else {
    print foreach sort keys %results;

sub permute {
    my $last = pop @_;
    return map [$_], @$last unless @_;
    return map { my $left = $_; map [@$left, $_], @$last } permute(@_)


=head1 NAME -- A simple tool to generate permutations of items


B< --help> for more information
    perl [options]


    --help    Display help
    --?       Same as --help
    --infile  File with list of items (one on each line)
    --outfile File to write permutations to
    --count   Number of items in permutation


This program will generate a list of all possible permutations of data
+ items
listed in an infile.  It will limit the number of items in the permuta
+tions to
the number of items specified in the C<--count> option.  For example, 
+with the
following items in an input file:


If you want to print all permutations with a limit of two items per
permutation, you would use the following command (assuming that the in
+put and
output files are named C<infile.txt> and C<outfile.txt>, respectively)

 perl --count 2 --in infile.txt --out outfile.txt

You may use shortcuts, too:

 perl -c 2 -i infile.txt -o outfile.txt

The above command will result in the following output:

If C<--count> is omitted, the count will default to the number of item
+s in the

If C<--out> is omitted, the results will be printed to C<STDOUT>.

Comment on Permutations, Getopt::Long, and Pod::Usage
Download Code
Replies are listed 'Best First'.
Re: Permutations, Getopt::Long, and Pod::Usage
by parv (Priest) on Feb 06, 2003 at 06:27 UTC
    And i thought, going by the title, that the post was going to be about permutations related to Getopt::Long module, Pod::Usage thrown in for good measure! Darts!

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://232927]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-11-28 18:34 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (743 votes), past polls