chinamox has asked for the wisdom of the Perl Monks concerning the following question:

Greetings again good monks,

I have the sinking feeling that this is going to be a silly newbie question, but I have gone hunting thru the perldocs and supersearch and I can't find a ready answer, so here goes...

I spent a good chunk of today writing a program for an online class and now I have run into a silly little gremlin. I want to give the user the option of calling a striphtml subroutine, but for some reason when I input this:

myusername$: perl myprogram.pl --striphtml someuserinput

My program runs the main subroutine and not the striphtml subroutine. But if I delete main(), then that same command line input runs fine.

Basically all, I want is to have sub main to run if there are no Getopt::Long arguments and conversely, only have sub striphtml run when the user specifies that option.

This is the basic layout of the program

#!/opt/bin/perl -w # # use strict; use Getopt::Long qw(GetOptions); use Pod::Usage qw(pod2usage); my $vars; my @morevars; GetOptions("striphtml" => sub { striphtml() }); main(); exit(); sub main{...} sub striphtml{...}

Thank you for your time and help

-mox

Replies are listed 'Best First'.
Re: A quick Getopt::Long question
by jdporter (Paladin) on Nov 22, 2006 at 15:07 UTC

    I agree with philcrow's answer; but here's an alternative which is more like how I'd write it.

    my $sub_to_run = \&main; GetOptions( 'striphtml!' => sub { $sub_to_run = \&striphtml } ); $sub_to_run->();
    We're building the house of the future together.
Re: A quick Getopt::Long question
by philcrow (Priest) on Nov 22, 2006 at 14:50 UTC
    Does striphtml end with an exit statement? If not, execution will fall through and run main too.

    Phil

      It does now. Thank you for the pointer!

      - mox
Re: A quick Getopt::Long question
by Anonymous Monk on Nov 22, 2006 at 14:48 UTC
    my $strip = 0; GetOptions(striphtml => \$strip); if ($strip) {striphtml()} else {main()}
    Or.
    my $strip = 0; GetOptions(striphtml => sub {striphtml(); $strip = 1}) main() unless $strip;

      I thank you for your help, oh nameless one. Both of these seem to do the trick fine.

      -mox