Página 1 de 1

Ayuda con ciclos while

NotaPublicado: 2017-04-20 10:50 @493
por Blue_Shell
Estaba programando, practicando los ciclos while cuando se me ocurrió emular lo que sería una shell. Una buena manera de hacer un ciclo while que haga que el prompt vaya saliendo siempre, pero llegamos a un problema, y es que se me ocurrió poner unos comandos por defecto, y al introducirlos funcionan, pero al salir otra vez el prompt y meter otro comando sale exactamente lo mismo que el primero pese a ser diferentes:
Sintáxis: [ Descargar ] [ Ocultar ]
  1. C:\Users\TheSt\Desktop\proyecto firestorm>perl init.pl 
  2. Bourne Shell version 1.0 started at 17:45:59 by 
  3. [Bourne Shell]>> #DefCommands 
  4. #First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About 
  5. [Bourne Shell]>> #First_time 
  6. #First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About 
  7. [Bourne Shell]>> 

El código es este:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use POSIX ;
  3.  
  4.  
  5. my $SH_VERS = "1.0" ;
  6. my $SH = "Bourne Shell" ;
  7. #my $USER = POSIX::cuserid();
  8. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  9.  
  10. printf ("$SH version $SH_VERS started at %02d:%02d:%02d by $USER \n", $hour, $min, $sec);
  11. print "[$SH]>> " ;
  12. my $CMD = <STDIN> ;
  13. chomp $CMD ;
  14. while ($CMD ne "#Exit") {
  15.         if ($CMD eq "#First_time") {
  16.                 print "Hi!, so you are new here!, well, this shell....\n" ;
  17.         }
  18.         elsif ($CMD eq "#DefCommands") {               
  19.                 print "#First_time , #DefCommands , #Mkdir , #Preview , #Exit , #Shutdown , #Restart , #Documentation , #License , #About \n" ;
  20.         }
  21.         print "[$SH]>> " ;
  22.         my $CMD = <STDIN> ;
  23.         chomp $CMD ;
  24. }      
  25.  
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4

He buscado información por Internet, en foros especializados y no he conseguido hallar la respuesta. He probado poniendo "next" en cada if y elsif pero solo consigo que la salida salga sin final:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Hi!, so you are new here!, well, this shell....
Terminating on signal SIGINT(2)
Hi!, so you are new here!, well, this shell....
 
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4

Un saludo y ¡gracias por adelantado!

Re: Ayuda con ciclos while

NotaPublicado: 2017-04-20 11:29 @520
por explorer
Bienvenido a los foros de Perl en Español, Blue_Shell.

El problema está en la función my() de la línea 22.

En esa línea estás creando una nueva variable local, $CMD, que almacena el valor leído desde el teclado, pero que desaparece al llegar a la línea 24. Por eso, la variable $CMD declarada en la línea 12 nunca se modifica.

Quita el my de la línea 22.

Puedes hacer que Perl te avise de estos errores, con solo añadir

use warnings;

al principio del programa.

Re: Ayuda con ciclos while

NotaPublicado: 2017-04-20 12:00 @542
por Blue_Shell
Wow, eso fue rápido xD

Muchas gracias, no me imaginé que fuera algo tan sencillo, lo tendré en la cabeza para la próxima vez, muchísimas gracias de nuevo y un saludo.