Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $var = 500 and $var = '500'

Tags:

perl

In Perl, what is the difference between

$status = 500; 

and

$status = '500'; 
like image 511
ado Avatar asked Jun 27 '13 00:06

ado


Video Answer


2 Answers

Not much. They both assign five hundred to $status. The internal format used will be different initially (IV vs PV,UTF8=0), but that's of no importance to Perl.

However, there are things that behave different based on the choice of storage format even though they shouldn't. Based on the choice of storage format,

  • JSON decides whether to use quotes or not.
  • DBI guesses the SQL type it should use for a parameter.
  • The bitwise operators (&, | and ^) guess whether their operands are strings or not.
  • open and other file-related builtins encode the file name using UTF-8 or not. (Bug!)
  • Some mathematical operations return negative zero or not.
like image 197
ikegami Avatar answered Sep 21 '22 14:09

ikegami


As already @ikegami told not much. But remember than here is MUCH difference between

$ perl -E '$v=0500; say $v' 

prints 320 (decimal value of 0500 octal number), and

$ perl -E '$v="0500"; say $v' 

what prints

0500 

and

$ perl -E '$v=0900; say $v' 

what dies with error:

Illegal octal digit '9' at -e line 1, at end of line Execution of -e aborted due to compilation errors. 

And

perl -E '$v="0300";say $v+1' 

prints

301 

but

perl -E '$v="0300";say ++$v' 

prints

0301 

similar with 0x\d+, e.g:

$v = 0x900; $v = "0x900"; 
like image 22
jm666 Avatar answered Sep 22 '22 14:09

jm666