Difference between "52" and 52?
Guys perl is not as easy i thought its so confusing thing.I just moved to operators and I wrote some codes but I am unable to figure it out how the compiler treating them.
$in = "42" ;
$out = "56"+32+"good";
print $out,;
The output for above code is 88 and where does the good gone? and Now lets see the other one.
$in ="42";
$out="good52"+32;
print $out ;
and for these the output is 32开发者_如何学Python. The question is where does the good gone that we just stored in $out and the value 52 between the " "why the compiler just printing the value as 32 but not that remaining text.And the other question is
$in=52;
$in="52";
both doing the same work "52" not working as a text . becuase when we add "52"+32 it gives as 84. what is happening and
$in = "hello";
$in = hello;
both do the same work ? or do they differ but if i print then give the same output.Its just eating up my brain.Its so confusing becuase when "52" or 52 and "hello" or hello doing the same job why did they introduce " ".I just need the explaination why its happening for above codes.
In Perl, +
is a numeric operator. It tries to interpret its two operands as numbers. 51
is the number 51. "51"
is a string containing two digits, and the +
operator tries to convert the string to a number, which is 51, and uses it in the calculation. "hello"
is a string containing five letters, and when the +
operator tries to interpret that as a number, it equates to 0 (zero).
Your first example is thus:
$out = "56"+32+"good";
which is evaluates just like:
$out = 56 + 32 + 0;
Your print
then converts that to a string on output, and yields 88.
In perl, the +
operator will treat its arguments as numbers, and try to convert anything that is not a number to a number. The .
(dot) operator is used to join strings: it will try to convert its operands to strings if they aren't already strings.
If you put:
use strict;
use warnings;
At the top of your script, you would get warnings such as:
Argument "good" isn't numeric in addition (+) at ...
Argument "good52" isn't numeric in addition (+) at ...
Perl automatically reassigns a string value to numeric, if possible. So "42" + 10 actually becomes 52. But it cannot do that with a proper string value, such as "good".
In perl, a string in a numerical context (like when you use a +
operator) is converted to a number.
In perl, you can concatenate string using the .
(dot) operator, not +
.
If you use +
, perl will try and interpret all of the operands as numbers. This works well for strings that are number representations, otherwise you get 0. This explains what you see.
$in=52;
$in="52";
both doing the same work "52" not working as a text . becuase when we add "52"+32 it gives as 84.
The problem here is not with the variable definition. One is a string and the other a number. But when you use the string in a numerical expression (+
), then it will converted to number.
About your second question:
$in = "hello"
defines a string, as you expect;$in = hello;
will just copy the symbol hello (however it is defined) on to your variable. This is actually not "strict" perl and if you setuse strict;
in your file, perl will complain about it.
First off, give this a read.
Your problem is that the +
is a mathematical addition, which doesn't work on strings. If you use that, Perl will assume that you're working with numbers and therefore discard anything that isn't.
To concatenate strings, use .
:
$str = "blah " . "blah " . "blah";
As far as the difference between "52"
and 52
goes, there isn't one. Since nothing (commands, comments, etc.) in Perl can start with numbers, the compiler doesn't need the quotes to know what to do.
精彩评论