ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

Master Script: use strict; use warnings; my $cmd1="C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Accesso +ries/Calculator.lnk"; my $generatorDir = "c:/perl/perl_tests"; my @rprtname = ("gui_calci"); my $rpcount=0; foreach(@rprtname) { my $rptscript = $rprtname[$rpcount]; system ("perl $generatorDir/$rptscript.pl")and die"error running $rpts +cript.pl $?" ; $rpcount++ }
gui_calci Script: use Win32::GuiTest qw(:ALL); use strict; system($cmd1); my @window=FindWindowLike(undef,"Calculator"); if(!@window) { die("cannot find window with title/caption Calculator\n"); } else { printf("window handle of Calculaotr is %x \n",$window[0]); } EnableWindow($window[0],1); PushChildButton($window[0],132); sleep 2; PushChildButton($window[0],93); sleep 2; PushChildButton($window[0],135); sleep 2; PushChildButton($window[0],121); sleep 2;

When I try to pass file path of calculator in master file itself my script does not work while when I pass it in gui_calci script it works fine. how can I pass file paths in the master script itself???

Replies are listed 'Best First'.
Re: How to pass file paths in a master script which is used to run several perl scripts?
by choroba (Cardinal) on Oct 18, 2016 at 09:51 UTC
    Pass the path as a parameter to the script:

    Master:

    my $path = '/a/b/c'; system 'perl', 'slave.pl', $path;

    Slave:

    my $path = shift; print "Path received from master: $path\n";

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Nice, another little trick I didn't know. That shift defaults to working on @ARGV in certain cases, I always would have assumed @_ had I not been confused at first seeing you do this choroba.

      shift

      Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If there are no elements in the array, returns the undefined value. If ARRAY is omitted, shifts the @_ array within the lexical scope of subroutines and formats, and the @ARGV array outside a subroutine and also within the lexical scopes established by the eval STRING , BEGIN {} , INIT {} , CHECK {} , UNITCHECK {} , and END {} constructs.
      I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
      I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

      Thanks a lot for the help! appreciate it but If there are 10-20 number of paths which I need to pass on to some 50's of files through the master script. shift can't be used effectively then I guess, any other efficient solutions for that??

        Consider using a config file and read the values within the child scripts, depending on whatever criteria you have to work with. If you have that many path variables it's worth thinking about this rather than managing then all within a series of scripts.

        If the provided filenames differ between separate child scripts:

        You can pass the filenames via command line and simply iterate over @ARGV to process them. If the length of the command is getting too long then you could provide the filenames by piping them into the childprocess via STDIN. Or you can write a temp file in the parent script and provide the child scripts with the filename to read from.

        2 possibilities come to my mind at the moment:

        If the file paths are not too long, you can pass them as additional parameters to system:

        # master my @files = qw[ path/to/file0 path/to/file1 path/to/file2 ]; system 'perl', 'slave.pl', @files;
        # slave print "List of files to process:\n"; print " $_\n" for (@ARGV);

        If the paths are too long to pass them all as parameters, write them to a file, 1 per line:

        # master my @files = qw[ path/to/file0 path/to/file1 path/to/file2 ]; open my $fh, '>', 'filelist'; print $fh, "$_\n" for (@files); close $fh; system 'perl', 'slave.pl'
        # slave open my $fh, '<', 'filelist'; print "List of files to process:\n"; print while <$fh>; close $fh;
Re: How to pass file paths in a master script which is used to run several perl scripts?
by marto (Cardinal) on Oct 18, 2016 at 09:32 UTC

    Again, you didn't post the error message:

    Global symbol "$cmd1" requires explicit package name at d:/pm/gui_calc +i.pl line 3.

    You have use strict; but don't define $cmd1. strict, system.

      Hi marto, Yes I Know that and that exactly is my doubt. I've defined $cmd1 in my Master script as u can see above in my code and I am trying to run my gui_calci script through master script. If I have 50 Scripts I need to run through a master script and each of those scripts contain a filepath, I want to pass all those file paths from master script or someway or another from outside because it is very difficuilt to modify filepaths in 50 scripts everytime . Any suggestions would help? P.S. Apologies for not posting the error message

        It doesn't work this way. In the master script you define your variables, however you run another perl script via system. It knows nothing about the variables in the master script. Consider the following example: master.pl

        #!/usr/bin/perl use strict; use warnings; my $secret = 'BruceWayneisBatman'; my @cmd = ('perl', 'child.pl', $secret); system(@cmd) == 0 or die "system @cmd failed: $?";

        child.pl

        #!/usr/bin/perl use strict; use warnings; my $message = $ARGV[0]; print "The master script passed: $message\n";

        Again see system.