http://qs1969.pair.com?node_id=11136430


in reply to Re^2: Automating execution of shell scripts
in thread Automating execution of shell scripts

There are so many errors in your subroutine that we are not sure what you are trying to do. I recommend that you study the documentation perlsub. I suspect that you are confusing us (and yourself) with a poor choice of variable names ($script1 for the name of a script file and $script2 for its output???). Given the code you have shown, $_ should not contain anything. You probably do not need a subroutine if all it does is extract the directory name from the script output. Use a regex with capture parenthesis in your main program.

We can provide much better help if you post a short program that we can run and duplicate your problem. Use use strict; use warnings and fix all errors and warnings that they cause. (Read the tutorial Short, Self-Contained, Correct Example)

Bill

Replies are listed 'Best First'.
Re^4: Automating execution of shell scripts
by wisely (Initiate) on Sep 20, 2021 at 07:51 UTC
    Sorry I forgot to mention this was only a snippet of the whole program, and it wasn't exactly accurate to what I had in actual program.
    I just wanted to illustrate what the direction in which I was headed for solving this.
    Sorry also that I couldn't just paste the whole code.
    Anyway in my actual program it turned out that the issue was the scope of the $dir variable in my subroutine as far as I can tell.
    The program works correctly now.
    I guess it would be better (also faster?) if it is done in the main program and not in a subroutine, but it was easier for me to follow what I am doing by writing subroutines. I am also not sure how to do what you mention.
      I guess it would be better (also faster?) if it is done in the main program and not in a subroutine ...

      A highly questionable guess IMHO. Reasonable factoring of code into subroutines certainly has the benefits of increased clarity and maintainability, encapsulation, etc., as you mention. The only speed advantage of inlining might be in a case in which you are calling a relatively simple function a gazillion times in a program. In the typical case, Perl call-return overhead vanishes into the background of a script.

      (And yes, please see Short, Self-Contained, Correct Example.)


      Give a man a fish:  <%-{-{-{-<

        Hi Bill and AnomalousMonk,
        I will make sure to review this and keep it in mind for the next time I need help on some code and come back to post here.
        In any case thank you all for the help with resolving the issues with the code.
        Cheers guys!
      We almost never want your complete program. The code that you extract should be made into a complete program which includes enough data that we can run it and duplicate your problem. This can be harder than it sounds. Often this is exactly what you need to solve the problem yourself. I suspect that this is what happened to you.
      Bill