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

I am converting a DOS script (quite a few actually, but this one is representative), and part of it changes the working directory to execute a piece of proprietary software. It then passes to that software certain execution parameters. I am not sure how to do the same thing in Perl, if it can be done at all.

*** start of script excerpt *** pushd %GTDIR% %GTSTARTUP_T% transaction=t(orgrole,trx_distrbtr,marketer_add_all,uk_e +nglish,[input_file="""%DATADROP%\marketer\LT415SQL.txt"""]) %GTID% popd *** end of script excerpt ***

Can anyone help me with this? I am REALLY new to Perl, and am not a programmer, so I'm really struggling with this. I would greatly appreciate any help someone might be able to provide.

Replies are listed 'Best First'.
Re: passing parms to other software program
by graff (Chancellor) on Apr 20, 2006 at 22:35 UTC
    So, if you are switching from DOS "batch" scripts to Perl scripts, then:
    • everywhere the batch file has "%PARAM%", the perl file will have $param

    • everywhere the batch file does "cd some\path" the perl file will do  chdir "some/path";

    • if the batch file does a lot of "pushd" (especially if it does two or more of these before doing a "popd"), the perl file should have  use Cwd; and  my @dirstack; near the top. Then everywhere the batch file does "pushd %SOMEDIR%", the perl file will do  push @dirstack, getcwd(); chdir $somedir; and everywhere the batch file has "popd", the perl file will have  chdir pop @dirstack;

    • where the batch file is putting literal strings on a command line, and presenting literal quotation marks as a string of three double-quotes ("""in quotes"""), the perl file will assign a quoted string to a variable or array element, and use a backslash to escape a quotation mark that should be part of the string:  $arg = "opt=x(foo,[bar=\"in-quotes\"])";

    As indicated above, do  system( @cmd ); or  system( $cmdname, @args ); to actually run some other program from the perl script.

    There might be ways to refactor or rethink the design of the batch files, to make better (shorter, easier to maintain) perl scripts, but if you're not a programmer, I don't know how far you'd be able or willing to go in that direction.

Re: passing parms to other software program
by zentara (Cardinal) on Apr 20, 2006 at 21:47 UTC
    Yeah, you can pass parameters to system or exec. There are some fine points about parameters on windows and dos(which I don't use), but try your stuff and report what errors you get.
    #!/usr/bin/perl use strict; use warnings; my $param1 = 'whatever'; my $param2 = 'whoever'; my $programtorun = 'program_name'; my @parameters = ( $param1, $param2 ); my @cmdline = ( $programtorun, @parameters ); system (@cmdline); # will run cmd, and then return to script #exec (@cmdline); # will replace currently running script

    I'm not really a human, but I play one on earth. flash japh
        in most cases it is better to generate a temporary file with File::Temp, write the output in that file (e.g. system($cmd . " > $tmp_fn") ) and then parse the output file. this allows memory efficent parsers.
      Thanks, zentara. I'll give that a shot.
Re: passing parms to other software program
by eff_i_g (Curate) on Apr 20, 2006 at 21:40 UTC
    You can change to a directory with chdir and call another script with system.