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

NEWBIE QUESTION:
I have a data entry script that runs once and exits. Is there any (simple) way I can have the script ask if you want to enter more data and then restart the script?? I know how to prompt for the info and get it I just don't know how I could get the script to then re run itself.

Thanks,
d00f

Replies are listed 'Best First'.
Re: Getting a script to re-run itself
by Masem (Monsignor) on Jun 26, 2001 at 01:27 UTC
    Simply wrap the entire thing up in a while( $keep_running ) { } block, with $keep_running set to 1 initially, and if the user doesn't want to continue, set it to zero.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Getting a script to re-run itself
by runrig (Abbot) on Jun 26, 2001 at 01:56 UTC
    While its probably better to wrap the whole thing in a while loop, and put something like this in the loop:
    last if $all_done;

    Another idea would be to just put this at the end of the script:

    exec '/my/script' unless $all_done;
      Even better would be to use $0 as the parameter to exec.
      exec $0 unless $all_done;
      That way the script can move around on the filesystem and you won't need to hand edit it.

      A warning: the above doesn't work correctly under Windows. For some reason, exec $0 spawns another copy of your script in the background. If you write a small test program like:

      print "hi!\n"; sleep 2; exec $0;
      and run it, you get the effect of:
      H:\>test.pl hi! hi! H:\>hi! hi! hi!
      What exactly it is doing, I don't know :)

      mr.nick ...