首次运行do-while语句后,perl脚本不打印

djmepvbi  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(173)

我正在尝试使用Perl为服务器中的一个简单菜单编写一个脚本,该菜单有三个选项,范围从1到3,如果您输入任何其他数字,它将提示您输入一个从1到3的数字。代码如下:

sub main_menu ()
{
        system('clear');
        print ('########## BIENVENIDOS A TERMINAL SERVER DE GOSIT ##########\n\n\n\n\n
            Seleccione una accion:\n\n
                1) Administracion de dispositivos\n
                2) Gestion de backups\n
                3) Salir del Terminal Server\n\n\n
        Ingrese opcion:   ');
        $action = <>;
        do
        {
                if ($action < '1' || $action > '3')
                {
                        print (" Por favor seleccione una opcion valida:    ");
                        $action = <>;
                        print ("\n\n");
                }
                elsif ($action == '1')
                {
                        #admin_menu();
                        print ("to admin menu\n");
                }
                elsif ($action == '2')
                {
                        #backup_menu();
                        print ("to backup menu\n");
                }
                elsif ($action == '3')
                {
                        print ("Saliendo de TERMINAL SERVER\n\n");
                        exit
                }
        } until ($action == '1' || $action == '2' ||$action == '3' );
}

main_menu();

在测试此脚本时,如果用户输入大于3或小于1的数字,则会提示错误消息,但如果稍后输入任何输入值,则脚本会结束,以下是我所做的测试:


指令集

llmtgqce

llmtgqce1#

问题在于,当你把retry函数放在if子句中时,循环控制语句和print语句分开了,那么执行将从第一个if子句继续,跳过所有其他的elsif,直接跳到末尾,在那里它将满足until并退出。
你也试着在一个单引号字符串中使用\n。那是行不通的,你需要一个双引号字符串。
您需要的是只有一个read语句,并且在读取失败时重试整个循环。

use strict;
use warnings;

sub main_menu {
        while (1) {     # loop can only be exited manually
            system('clear');
            print '########## BIENVENIDOS A TERMINAL SERVER DE GOSIT ##########\n\n\n\n\n
            Seleccione una accion:\n\n
                1) Administracion de dispositivos\n
                2) Gestion de backups\n
                3) Salir del Terminal Server\n\n\n
        Ingrese opcion:   ';
            my $action = <>;
            no warnings 'numeric';    # disable numeric warnings

            if ($action == 1) {
                    print ("to admin menu\n");
            } elsif ($action == 2) {
                    print ("to backup menu\n");
            } elsif ($action == 3) {
                    print ("Saliendo de TERMINAL SERVER\n\n");
                    exit
            } else {
                    print (" Por favor seleccione una opcion valida.\n");
            }

            print "Press any key to continue...";    # needed to be able to read message
            <>;                                      # for pausing the screen
        };
}

main_menu();

注意no warnings 'numeric'加法。这段代码要求你在运行时关闭警告,否则你会收到关于数值比较的警告:

Argument "asd" isn't numeric in numeric eq (==) at ...

一种解决方法是清理输入并强制其为数值型,即使它不是。

$action =~ s/\D+//g;          # remove non-numbers
$action = 0 unless $action;   # if "" turn it into 0

或者你可以忽略这些警告

no warnings 'numeric';

相关问题