• 0

[Perl] $_ and pattern matching


Question

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

  • 0

$_ is simply the default thing to match against.

/0/ is just matching against $_ since it wasn't specified to match against something else.

if ($num =~ /0/) has the same meaning as /0/, it's just more wordy and Perl coders love to do things their own little hacky way.

Link to comment
Share on other sites

  • 0
$_ is simply the default thing to match against.

/0/ is just matching against $_ since it wasn't specified to match against something else.

if ($num =~ /0/) has the same meaning as /0/, it's just more wordy and Perl coders love to do things their own little hacky way.

But why wouldn't removing the slashes make it the same as ($_ == 0)?

Link to comment
Share on other sites

  • 0

That's not a very good book if it threw out a sample like that without explaining the Perl special variables (there's more to Perl's shorthand than that, so it would help if you familiarize yourself with the documentation on them).

Link to comment
Share on other sites

  • 0

True Clinteger, but I don't think that's all that he's asking. I think he's confused about the operators.

You can't do something like

0 && print "zero";

In perl because 0 is just a number where as /0/ is an implied operation (operating on 0).

You shouldn't be overriding $_ anyways, especially without using local. It's bad practice. Read perlvar, because it will save you a lot of headache later on.

Link to comment
Share on other sites

  • 0
You can't do something like

0 && print "zero";

In perl because 0 is just a number where as /0/ is an implied operation (operating on 0).

Ohh okay that makes since. Thanks, I'll read up on the predefined variables.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.