Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Running perl functions from a command line

by wizard341 (Acolyte)
on Jan 28, 2003 at 14:34 UTC ( #230591=perlquestion: print w/replies, xml ) Need Help??
wizard341 has asked for the wisdom of the Perl Monks concerning the following question:

Im having a bit of trouble trying to find documentation on running perl functions via the windows comand line. I have a set of functins, say Foo1(), Foo2(), and Foo3() that i would like to be able to run if i type something like - a or whatever. Actually im writing up a batch file in which to supply the switches, doubt that matters though. Does anyone have any good links or tips to getting it to run? Thanks!
  • Comment on Running perl functions from a command line

Replies are listed 'Best First'.
Re: Running perl functions from a command line
by MZSanford (Curate) on Jan 28, 2003 at 14:52 UTC

    If you are looking for something to run specific function based on program arguments, like -foo1, you may want to look at the Getopt:: namespace (I like Getopt::Long, but Getopt::Std works for single switches) for argument processing.

    Once you have one of the Getopt:: modules working, whichever one, you may want to look at pl2bat (some info here) for making a bat file out of your perl code.

    from the frivolous to the serious
Re: Running perl functions from a command line
by AcidHawk (Vicar) on Jan 28, 2003 at 14:57 UTC

    Have you looked at Getopt::Simple or the Getopt::Long

    These will handle your script with -a or --a options.

    Below is a snippet of an example which came from a Win32 service script.

    use Getopt::Long; my %Config = ( timeout_value => 2, log_file => join( "", Win32::GetFullPathName( $0 ) ), ); $Config{log_file} =~ s/[^.]*?$/log/; Getopt::Long::Configure( "prefix_pattern=(-|\/)" ); $Result = GetOptions( \%Config, qw( install|i remove|r timeout_value|t=i log_file|l=s monitor_sub_dirs|s account_id|user=s account_password|pass=s help|?|h ) ); $Config{help} = 1 if( ! $Result || scalar @ARGV ); if( $Config{install} ) { Install(); exit(); } elsif( $Config{remove} ) { Remove(); exit(); } if( $Config{help} ) { Syntax(); exit(); }

    Of all the things I've lost in my life, its my mind I miss the most.
Re: Running perl functions from a command line
by hardburn (Abbot) on Jan 28, 2003 at 14:55 UTC

    You need to parse the command line options (see Getopt::Long) and then run the functions based on that. For instance:

    use strict; use Getopt::Long; my $a_opt; my $options = GetOptions('a' => $a_opt); if($a_opt) { Foo1(); Foo2(); Foo3(); } # Rest of program here
Re: Running perl functions from a command line
by talexb (Canon) on Jan 28, 2003 at 14:55 UTC

    If you want to run each function separately, put them each into their own script, and run the script from a batch file.


    perl -w %1 %2 %3

    #!/usr/bin/perl -w # sub foo { my ( $var1, $var2, $var3 ) = @_; # More code follows, using the passed parameters. }

    That should work.

    --t. alex
    Life is short: get busy!

    Update:: Added parameter list on Perl script.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://230591]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-02-24 01:06 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.