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

i want to write a piece of code say comp.pl where i can pass one or more comma separated directories into a file and use that as INPUT to a called script runTest.pl .

here, directories contains multiple files having testcases

below, $runPath -> path for testcases file / directory containing such file

$runTest -> command line passed testcases file or command line passed directory where such multiple testcases files exist

my $testFile = ''; if (-f "$runPath/$runTest") { $testFile = "$runPath/$runTest"; } else { open (my $RF, '>', 'testFile'); print {$RF} map { "$_\n" } split(/,/, $runTest); close $RF; $testFile = "$runPath/$testFile"; } $status += utils::execCommand(Command => "$runTest.pl -f $testFile;

the above $status line is working fine when passing testFile from command line but not working when passing directory/directories.

example: comp.pl --runTest testDirectory1, testDirectory2

so when i am executing the comp.pl script, it is not reading the files and its listed testcases under testDirectory1 and testDirectory2. it is just printing testDirectory1 and testDirectory2 line by line inside <testFile>

  • Comment on pass one or more directories into a file and use that as INPUT to a called script
  • Download Code

Replies are listed 'Best First'.
Re: pass one or more directories into a file and use that as INPUT to a called script
by hippo (Archbishop) on Aug 13, 2020 at 15:33 UTC

    Nowhere in your code are you processing the contents of the directories. See the Q&A How do I read a directory? for examples of how to do that.


    🦛

Re: pass one or more directories into a file and use that as INPUT to a called script
by tybalt89 (Monsignor) on Aug 13, 2020 at 16:17 UTC

    Were you looking for something like this?

    #!/usr/bin/perl use strict; use warnings; my $runTest = 'one.d,ants,mail,3chess.pl,two.d'; sub dive { map { -d $_ ? dive( glob "$_/*" ) : $_ } @_ } my @files = map { dive($_) } split /,/, $runTest; print "$_\n" for @files;
Re: pass one or more directories into a file and use that as INPUT to a called script
by wazat (Monk) on Aug 13, 2020 at 23:06 UTC

    From your code, in the 2nd case, you do not specify where a file named 'testFile' is created. It will be created in the current working directory, which may or may not be what you intended.

    Also in the second case "$testFile" will be set to something that looks like a directory, "$runPath/". Does "$runTest.pl" expect this for its -f argument? From your description I have no idea what "$runTest.pl" is supposed to do.

    I suspect that the other commentators are on the right track in that you intent was to read the files in the specified directories.

    It might also be useful if you provided a more complete code snippet.

Re: pass one or more directories into a file and use that as INPUT to a called script
by perlfan (Parson) on Aug 13, 2020 at 14:58 UTC
    >it is just printing testDirectory1 and testDirectory2 line by line inside <testFile>

    Well, looks like that's what you told it to do here:

    print {$RF} map { "$_\n" } split(/,/, $runTest);

    If you want to get the contents of the directories, you'll need to checkout opendir or File::Find (and friends).

    For the best line of help, post more code for context. Particularly your options processing. And be sure to use strict; and use warnings;.

    Oh,

    open (my $RF, '>', 'testFile');
    Did you mean,
    open (my $RF, '>', $testFile);