Hi monks,
I don't know implicit type exchange is a feature or a drawback of perl.In terms of me,I fall the same trap again and again ;)
Some days ago, many monks tell me oct and hex function only accept string no matter what you input.
here
Yesterday,I wrote a script for fun and practice.
here
I found that user input 1(5 spaces before number) is equal to 1.Because of the last experience,I suspect that implict type exchange resulted in it.So I have a try:
use strict;
use warnings;
my $num1 = 4;
my $str1 = "4";
my $str2 = " 4";
print "number \$num1 is equal to string \$str2!!\n"if $num1 == $str2;
print "number \$num1 is equal to string \$str1!!\n" if $num1 == $str1;
print "string \$num1 is equal to string str2 !!\n" if $num1 eq $str2;
print "string \$num1 is equal to string str1 !!\n" if $num1 eq $str1;
__OUTPUT__
number $num1 is equal to string $str2!!
number $num1 is equal to string $str1!!
string $num1 is equal to string str1 !!
Obviously,perl will truncate all spaces before number when it treats a string as a number.No offense,I was wondering why perl develper don't add type declaration(such as num,str).At least, It will help perl beginner not falling many pitfalls like above.
I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.