in reply to How to pass file paths in a master script which is used to run several perl scripts?

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,
  • Comment on Re: How to pass file paths in a master script which is used to run several perl scripts?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to pass file paths in a master script which is used to run several perl scripts?
by perldigious (Priest) on Oct 18, 2016 at 13:55 UTC

    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
Re^2: How to pass file paths in a master script which is used to run several perl scripts?
by ankit.tayal560 (Beadle) on Oct 18, 2016 at 10:39 UTC

    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.

        Marto, Could you please explain a little bit more about this as I am a beginner to this part of perl scripting. I don't have an idea about how to make config files and all ?? any help is appreciated . Thanks in advance!

      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;