lmckenz lmckenz - 10 months ago 33
Perl Question

How do I close the CMD when I enter a certain value in perl?

I am currently making a program that will eventually be able to auto start when I sign onto my computer and will allow me to select from a variety of programs to start. I currently have two (just started it). Anyway, I was wondering, because it is being run thru CMD, how can I have the program close CMD when I enter a certain value. My code:

print "What would you like to run? (1 for Chrome and 2 for Calculator \"e\" for exit) \n";
my $prog_run = <STDIN>;

if ($prog_run == 1){
#Chrome Function
system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"');
}elsif ($prog_run == 2){
#Calculator Function
}elsif ($prog_run == "e"){
#Exit Fucntion
print "Exiting........";

}elsif ($prog_run == "e"){

== is the numeric comparison operator; it only works correctly on numbers. Since "e" is a string, you will need to use the string comparison operator, eq, to compare something with it:

} elsif ($prog_run eq "e") {

However, the line you read from standard input will have a newline on it by default. You can either include this in your string ("e\n"), or chomp $prog_run; beforehand to strip off the newline.


If you're in a loop, you can use last to exit out of it. Alternatively, you can use exit to make the whole Perl process… well, exit.