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

I've created a sub-routine to run a command so I can test it out with the timethese routine

however when it runs it outputs
sh: | ./perl/tester.pl: No such file or directory
the file is there but I can't figure out what's wrong

sub run { # creates data my $conf = `(cat ./BasicTest.opt; ls ./data/*_seq.xml)`; my $num = shift; # changes a setting $conf = s/99/$num/; # runs program return `"$conf | ./perl/tester.pl"`; }

Replies are listed 'Best First'.
Re: benchmarking problem
by Zaxo (Archbishop) on Jun 07, 2006 at 03:01 UTC

    I think you have the quotes a level too deep,

    return `$conf | ./perl/tester.pl`;
    You may be using the wrong quotes where you set $conf, but maybe not, I can't tell. You haven't described how those files are constructed and how they work together.

    After Compline,
    Zaxo

Re: benchmarking problem
by graff (Chancellor) on Jun 07, 2006 at 06:41 UTC
    Several problems: First, you are assigning the output of a backtick shell command to $conf, but not testing to see if the command worked. Depending on what happens to be in your current working directory, that shell command might return an empty string. Did you happen to notice anything else on STDERR (which this initial backtick shell would produce, if those commands failed)?

    (BTW, the error message you cited is caused by having an empty string to the left of the pipe symbol in a shell command.)

    In any case, this next part is almost certainly not what you really want:

    # changes a setting $conf = s/99/$num/;
    I think you meant to use the "=~" operator there (to alter the contents of $conf, assuming it's not empty)? As it is, when the substitution fails, $conf is be set to empty string. (It would be set to "1" on success, but that would only happen if by chance $_ contains "99").

    But even with the "=~" operator there, the next part doesn't make sense: if the initial backtick command worked as intended, $conf would contain a multi-line string. If you change a 99 in that string to something else, it's still multi-line, and I expect that putting it at the start of a pipeline in another backtick shell command would be a bad idea.

    (Update: added clarification in first paragraph, fixed grammar in the second; also wanted to point out that the initial backtick command (cat; ls) should probably be replaced by using perl-internal operations: open/read the file, use a file glob or readdir; this would make the error handling better.)

Re: benchmarking problem
by drawde83 (Acolyte) on Jun 07, 2006 at 22:31 UTC
    yep got it working just needed to follow your suggestions.
    I'll convert the backtick shell bit to use perl internal operations
    (I had it running as a shell command so it was easier this way)