Jump to content

Rock paper scissors game in Perl - Enjoy hope someone finds it useful


G+_Scott B
 Share

Recommended Posts

Hey Scott I tried your program and it works great but there are a few things I wanted to make you aware of. The first is that the non-numeric input test needs a "next;" statement in the if block after the print. This sends to program back to the top of the loop. Right now it continues on down and gets an error. The other thing is that you are using the wrong kind of operators in your "if" expressions. In Perl, the symbolic operators (>, <, etc.) are used for numbers and mnemonic ones (gt, lt, etc.) are used for strings. Your code is only working because Perl is automatically converting the numbers to strings when it sees the mnemonic operators and, because your numbers are only single digits, the test works properly. Comparing a single digit to a multiple-digit number would not work though. For example: (5 gt 12) returns True because the "gt' causes it to do a string comparison which starts with the left characters and the character "5" is greater than "1".

Link to comment
Share on other sites

interesting, thank you for your input. i didn’t get any error running the code,  even after adding use strict; & use warnings;  i only get "Global symbol "" requires explicit package name at" warning and this is fixed by adding "my" to the variable's

 

i have updated the code with the changes you point out, next seems to be more of a best practices than a requirement or at least that is the way i interpret the examples i looked at.

 

updated version http://pastebin.com/UhmEVefa

Link to comment
Share on other sites

You won't get any warnings because it's legal to do and useful in certain circumstances but the two types of operators are not interchangeable. (5 > 12) returns False, as one would expect, but (5 gt 12) returns True because it's doing a string comparison instead of a numeric one.  I just wanted to make sure you and the other Coding101 folks understand how it works.

Link to comment
Share on other sites

I understand the operator's and appreciate your comments and feedback, and as I don't see error there is no way I would have known I was using the wrong operator's

 

I was more interested in the next; error / command.

 

This was my first code in Perl please feel free to comment on any of my posts I we'll come the feed back

Link to comment
Share on other sites

 Share

×
×
  • Create New...