3

I am running a Perl script to control two other scripts. The user of the main program can call the two secondary scripts with the following line.

do 'Task_A.pl';

The script then runs Task_A.pl but I would like to find a way of then exiting Task_A.pl and return to the main program. If possible I would like to return to the function where I last called the secondary script. I am not sure as to what this is called but I appreciate any input for a possible solution.

This is the whole main program, not much to it at the moment.

my $selecion;

#Looping variables.
my $program_loop = 1;

while ($program_loop == 1)
{
    print "Please choose one of the programs listed in the menu.\n";

    #Program menu where the user chooses from the presented options.
    print "[1] - Script A.\n";
    print "[2] - Script B.\n";
    print "[3] - Exit program.\n";

    my $user_input = <>;

    if ($user_input == 1) # <-- Scrip_A.pl
    {
        do 'Task_A.pl';
    }
    elsif ($user_input == 2) # <-- Scrip_B.pl
    {
        do 'Task_B.pl';     
    }
    elsif ($user_input == 3) # <-- Exit Program
    {
        #The user can choose to exit from the menu.
        print "The program will now exit.\n";
        exit;
    }
}
5
  • 2
    Programmers don't die, they just GOSUB without RETURN.
    – simbabque
    Aug 11, 2015 at 13:49
  • 2
    I think what you actually want to do is use function calls. do is most likely a terrible idea. Please show some more of the actual code. It's also not clear how the user (is that a programmer) of the main script would call a secondary script.
    – simbabque
    Aug 11, 2015 at 13:49
  • I thought so too. Using do is probably not the best way to go about but I am not really sure what my options are here. Aug 11, 2015 at 13:55
  • 1
    If there is no shared memory/variables necessary, start a new process. Alternatively, refactor your two programs into modules that share the same interface and use those. This will be hard if there is a lot of global legacy stuff going on. As your program does not have use strict and use warnings, I will assume that is the case. Why are you doing this exactly?
    – simbabque
    Aug 11, 2015 at 13:57
  • 2
    Generally speaking, you're doing something wrong if you use do.
    – ikegami
    Aug 11, 2015 at 14:31

1 Answer 1

3

do does not start another process. If that's what you want, use "system()".

If you want do, then it's probably best to just put everything into a function in the file to be evaluated and call the function at the end of the file.

Use "return" to leave the function and return to next instruction after do.

7
  • I tried that but somehow the Task_A.pl program just exits without returning to the main program. Aug 11, 2015 at 13:50
  • 2
    You need to understand what do does. It's not a fork or a sub-process, it just slurps in the file and runs it in place. Over and over again, if you decide to do so.
    – simbabque
    Aug 11, 2015 at 13:51
  • Yup, haven't used do in decades :-)
    – neuhaus
    Aug 11, 2015 at 13:56
  • I use it with BLOCKs. But not with files.
    – simbabque
    Aug 11, 2015 at 13:58
  • 2
    I think you can greatly improve the answer by formatting it a bit, linking to the perldoc for system and explaining why that works, with a small example. Note I have not downvoted it.
    – simbabque
    Aug 11, 2015 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.