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

Hi, When the user runs the script "script.pl"from command line as below,I am running an exe findfile.exe in that script based on the command line options.How do I process the command line and run findfile.exe based on the correct options(even though the options are interchanged,the script should pick the right options.Can someone pls help? >>perl script.pl -f file.c -d c:\ >>perl script.pl -d C:\ -f file.c

#!/usr/bin/perl -w use strict; use warnings; findfile.exe <opt_f> <opt_d>

Replies are listed 'Best First'.
Re: Processing command line based on user input
by roboticus (Chancellor) on Nov 20, 2010 at 04:51 UTC

    lphone:

    An EXE file name isn't a valid perl statement. If you want perl to run the program, you have several choices. You can wrap it in backticks, like this:

    #!/usr/bin/perl use strict; use warnings; my $opt_f = shift; # Do your option checking here... `findfile.exe $opt_f`;

    You could also use system("a command goes here"); to run it.

    ...roboticus

Re: Processing command line based on user input
by PeterPeiGuo (Hermit) on Nov 20, 2010 at 04:32 UTC

    Search for Getopt and Getoptions::Long

    A quick sample:

    use Getopt::Std; use Data::Dumper; $options={}; getopt("df",$options); print Dumper($options);

    Peter (Guo) Pei

      Running the above is giving the following output.Now how do I access these options and run my exe findfile? $VAR1 = { 'd' => 'C:\', 'f' => 'file.c' };

      #!/usr/bin/perl -w use strict; use warnings; `findfile.exe <opt_f> <opt_d>`
Re: Processing command line based on user input
by viveksnv (Sexton) on Nov 20, 2010 at 04:53 UTC
    Hi,
    You may check the arguments with if and modify it on the fly if needed and execute the exe.
    #!/usr/bin/perl use strict; use warnings; ##arguments chomp(my $first_arg = $ARGV[0]); chomp(my $second_arg = $ARGV[1]); ###check the arguments are correct if($first_arg ne "something") { #print some errors } if($second_arg ne "something") { #print some errors } ##execute the exe findfile.exe $first_agr $second_arg

      #!/usr/bin/perl use strict; use warnings; ##arguments chomp(my $first_arg = $ARGV[0]); chomp(my $second_arg = $ARGV[1]); ###check the arguments are correct if($first_arg ne "something") { #print some errors } if($second_arg ne "something") { #print some errors } ##execute the exe findfile.exe $first_agr $second_arg

      Did you actually try to run this nonsense? It won't even compile!

      perl 872641.pl Global symbol "$first_agr" requires explicit package name at 872641.pl + line 24. Bareword "findfile" not allowed while "strict subs" in use at 872641.p +l line 24. Execution of 872641.pl aborted due to compilation errors.

      chomping the arguments does not make any sense. External programs aren't executed by just writing their filename into the script. And even if they would, you would pass just the first two arguments to the external program, not all arguments as required. iphone explicitly asked for at least four arguments. And when called with less than two arguments, the external program would still be invoked with two arguments, one or both being empty strings.

      Please stop posting such junk, it will only frustrate people searching for help. Test your code before posting it.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)