I am reading a book on Perl, and I am a little confused about some of the code. The script is pretty simple: it takes an integer 0-9 as input and returns the spelling of that number.
#!/usr/bin/perl -w
while (){
print "Enter a number 0 - 9: ";
chomp ($num = <STDIN>);
if ($num =~ /^\d$/){ #if input is one digit
last;
}else{
print "Must be between 0-9.\n";
}
}
$_ = $num; #cleans up code to print responses
### Prints the spelling of the number entered ###
/0/ && print "Zero.";
/1/ && print "One.";
/2/ && print "Two.";
/3/ && print "Three.";
/4/ && print "Four.";
/5/ && print "Five.";
/6/ && print "Six.";
/7/ && print "Seven.";
/8/ && print "Eight.";
/9/ && print "Nine.";
print "\n";
I'm having a hard time understanding the block where it prints the spelled-out numbers. Why do you have to pattern-match instead of just checking if $num is equal to the integer? When I remove the slashes, the statement registers as false so it doesn't print, but when I write ($num == 0) && print "Zero.";, it works... I would think that I'd have to do that for /0/ too if I can't just write 0 without the slashes.
I'm obviously very confused and missing something here! My guess is that I'm confused about the concept of the _ && _ statements or the $_. As I understand, it evaluates the left side and, if true, evaluates the right. And I thought that by setting $_ = $num, /0/ is the same as ($_ =~ /0/) and 0 same as ($_ == 0). Perhaps I'm wrong about the $_ == 0.
Question
generalt
I am reading a book on Perl, and I am a little confused about some of the code. The script is pretty simple: it takes an integer 0-9 as input and returns the spelling of that number.
#!/usr/bin/perl -w while (){ print "Enter a number 0 - 9: "; chomp ($num = <STDIN>); if ($num =~ /^\d$/){ #if input is one digit last; }else{ print "Must be between 0-9.\n"; } } $_ = $num; #cleans up code to print responses ### Prints the spelling of the number entered ### /0/ && print "Zero."; /1/ && print "One."; /2/ && print "Two."; /3/ && print "Three."; /4/ && print "Four."; /5/ && print "Five."; /6/ && print "Six."; /7/ && print "Seven."; /8/ && print "Eight."; /9/ && print "Nine."; print "\n";I'm having a hard time understanding the block where it prints the spelled-out numbers. Why do you have to pattern-match instead of just checking if $num is equal to the integer? When I remove the slashes, the statement registers as false so it doesn't print, but when I write ($num == 0) && print "Zero.";, it works... I would think that I'd have to do that for /0/ too if I can't just write 0 without the slashes.
I'm obviously very confused and missing something here! My guess is that I'm confused about the concept of the _ && _ statements or the $_. As I understand, it evaluates the left side and, if true, evaluates the right. And I thought that by setting $_ = $num, /0/ is the same as ($_ =~ /0/) and 0 same as ($_ == 0). Perhaps I'm wrong about the $_ == 0.
Link to comment
Share on other sites
5 answers to this question
Recommended Posts