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

Hello Monks,
At first here is part of my code:
&pytaj(); if ($question1 eq '') { print "Nie wybrales zadnej opcji \n"; print "Zamykam.... \n"; sleep(5); exit(); } elsif ($question1 eq "1") { &backup(); } elsif ($question1 eq "2") { &restore(); } elsif ($question1 eq "3") { &testowanie(); } elsif ($question1 eq "4") { &full(); } elsif ($question1 eq "5") { &shared(); } elsif ($question1 eq "6") { &archive(); } elsif ($question1 eq "0") { print "Ok wiec koniec pracy. Zamykam.... \n"; sleep(5); exit(); } sub pytaj { print "*************** \n\n"; print "Narzedzie do wykonywania operacji na bazach danych i plikach SM +/PCB klientow CORE \n"; print "Jesli nie jestes pewien co robisz, do czego sa te opcje to lepi +ej nie rob nic! \n"; print "Wybierz zadanie do wykonania: \n\n"; print "[1] utworz backup bazy do pliku gbk \n"; print "[2] odtworz baze ze wskazanego pliku gbk \n"; print "[3] przetestuj wskazana baze danych \n"; print "[4] wykonaj kompletna procedure backupu - backup, odtworzenie i + testowanie \n"; print "[5] zarchiwizuj katalog Shared do zip \n"; print "[6] spakuj podana baze do pliku zip \n"; print "[0] Nie rob nic i wyjdz \n\n"; print "Plik do przetworzenia podawaj jako pelna sciezke do pliku! \n\n +"; print "Podaj numer czynnosci do wykonania: "; chomp( $question1 = <STDIN> ); return $question1; }
TODO - script prints to STDOUT list of options, user select one, subroutine is executed.
At end of subroutine e.g.
sub shared { $sharedarchname = "shared-$day.zip" ; chdir($shared); $object2 = Archive::Zip->new(); $object2->addTree("$shared"); my $target; print "Do jakiego katalogu zapisac paczke [Jesli nie podasz b +edzie w $kopietar] : "; chomp( $target = <STDIN> ); $target = $kopietar if $target eq ''; if ($object2->writeToFileNamed("$target$sharedarchname") != 0) { print "Blad pakowania Shared! $! \n"; print "Automatyczne wyjscie za 10s..."; sleep(10); exit(); } else { print "Shared poprawnie zarchiwizowane. Zapisano w $target$sharedarchn +ame \n"; } &pytaj(); }
I want back to options menu so I`ve inserted &pytaj(); and menu is printed and waits for selection but when I type selection number and press enter the script ends but should go back to beginning.
I think there should be some simple method to go back to top of script - the $question1 should be returned to top and whole script should be executable once again. Finally it should be executable unless error breaks execution of any of subroutine (this is done by die where sub should break) or 0 - quit will be selected.
GOTO 10 didn`t work ;))
Idea - is there an option to end all of subroutines with back to beginning of the script? If I cut out sub pytaj to beginning of the script it will be correctly executed, $question1 reply launches correct subroutine but only one thing needed is to back after end of sub to beginning of script.

Replies are listed 'Best First'.
Re: How to escape from sub to beginning of script?
by choroba (Cardinal) on Mar 29, 2012 at 12:23 UTC
    Just enclose the call to &pytaj into
    until ($question1 eq '0') { pytaj(); }
    No exit would be then needed if the user specifies 0.
      This jails me in the options menu - whatever option user will select it prints the menu until user select 0 and it quit.
        OK, so add exit to other options :-)

        Or, use a different variable, e.g. $restart, and set it to 1 only if you want to restart the loop. The loop then becomes

        $restart = 1; while ($restart) { $restart = 0; .... }
        Or whatever similar.
        Ok got it. I`ve wrong inserted the until statement.
        Thanks for your help :).