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

Dear Monks,

How do I keep a script from going on before a subprocedure is done? I have a script in which a subprocedure actually does most of the work, so it takes a while, and it doesn't return anything... (I coded it as a subprocedure because there are two variants with different arguments). But I need do some cleaning up after the sub ends, and at the moment the "cleaning up" happens too early, so that the directories for the temporary data are deleted :-/

I'm a beginner in programming and I'm not sure how to approach this or what to google for. Do I need to introduce a return value as a "placeholder", or is there a more natural way?

  • Comment on Keeping a script from going on before a sub is done

Replies are listed 'Best First'.
Re: Keeping a script from going on before a sub is done
by roboticus (Chancellor) on Jun 24, 2012 at 20:07 UTC

    Normally, scripts don't go on until a subprocedure is done. But there are ways to make it do so, and you're most likely using one of them. However, you're not showing any code, so it's hard to tell what you're doing incorrectly.

    A couple things off the top of my head are:

    • You could be using forks or threads in your program.
    • You could be executing shell commands and explicitly putting them in the background (such as when using the '&' operator in bash).
    • There could be a timeout implemented (such as with signal and a timer) that's terminating the program earlier than expected.

    There are other ways, too, of course. If you'd show a bit of code, perhaps someone could help you identify the problem.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Just wanted to say thanks. I'll look for these and re-post with code if necessary.
Re: Keeping a script from going on before a sub is done
by CountZero (Bishop) on Jun 25, 2012 at 06:44 UTC
    Or you could use a semaphore file: when finished, the sub-procedure places a file somewhere at an agreed place and the main program watches until the file appears and then does the clean-up (including deleting the semaphore file).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Thanks, this looks like a promising idea :-) Actually, I think I might even have files I can use as semafores. I only need to restructure the script...