Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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 myprogram.pl - 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
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 myprog.pl -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 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.

    foo.bat:

    perl -w foo.pl %1 %2 %3

    foo.pl:

    #!/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.

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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://230591]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-10-31 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls