[Edinburgh-pm] a Perl surprise

Aaron Crane perl at aaroncrane.co.uk
Wed Jul 18 08:06:38 PDT 2012


Jeff Parsons <bynari at gmail.com> wrote:
> You said:
>
>  wtf qw(6 7 8)
>
> is equivalent to:
>
>   wtf (('6', '7', '8'))
>
> That's actually incorrect and wouldn't be important anyway. '6' is 6 as a
> string, 6 is 6 as a number, they're not strictly the same thing.

Hi, Geoff.  I'm not sure I understand the point you're making here.
The behaviour of qw(6 7 8) is the same as the list '6','7','8' as far
as I can tell.  In particular, the values you get from qw(6 7 8) are
internally treated as strings (unless and until you try to use them as
numbers), just as the values in '6','7','8'.  With 6,7,8, on the other
hand, the values are internally treated as numbers (unless and until
you try to use them as strings).  You're right, though, that there are
relatively few situations where Perl cares about the distinction
between the number 6 and the string '6'.

You can use Devel::Peek to see the difference:

use Devel::Peek 'Dump';
my @q = qw(6 7 8);
my @s = ('6', '7', '8');
my @n = (6, 7, 8);
Dump(\@q);
Dump(\@s);
Dump(\@n);

-- 
Aaron Crane ** http://aaroncrane.co.uk/


More information about the Edinburgh-pm mailing list