in reply to Re^2: result is not listening to if
in thread result is not listening to if

I though that if you put double quoutes around a variable value in Perl, Perl would know for example that "20" is a number. And that if you put variable value between single quotes like this "my $number = '20'. Perl would take it as a literal string.

I think you may have misread or misinterpreted something. "20" is exactly the same as '20' in perl. But if $z has value 20 then "$z" and '$z' are quite different.

#!/usr/bin/env perl use strict; use warnings; my $dql = "20"; my $sql = '20'; my $z = 20; my $dqv = "$z"; my $sqv = '$z'; print "dql: $dql\n"; print "sql: $sql\n"; print "dqv: $dqv\n"; print "sqv: $sqv\n";

The assignment to $dqv is what is known as interpolation. Double quotes do it but single quotes do not. See the "interpolates" column in the table in Quote and Quote-like Operators for the full list.

Note that by putting any sort of quotes around a numerical value you are essentially converting it into a string. $z above is a pure number the other variables are all strings. But Perl isn't strongly typed so you can get away with plenty of mixing and matching.

Addendum: The choice of variable names in this example might not be obvious. They are abbreviations for "double quoted literal", "single quoted literal", "double quoted variable" and "single quoted variable". (Thanks 1nickt for pointing out the lack of clarity).

Replies are listed 'Best First'.
Re^4: result is not listening to if
by prospect (Acolyte) on Jul 14, 2017 at 14:21 UTC

    Ok Thank you, so if I understand correctly, if I want to make sure Perl interprets my value as a number I need to not put quotes around it?

      It's a bit more nuanced than that because, as I say, Perl isn't strongly typed. However it's still something I would encourage as good practice because:

      1. It makes your intention in the code very clear
      2. It will certainly make a difference when you start working in bases other than decimal
      3. It doesn't hurt

      While learning you can always try quick one-liners to see what happens when you try something. Feel free to experiment on your own. If you see behaviour you don't understand try to find it in the documentation or ask about it here.