Monday, March 19, 2007

Perl quiz

What does the following Perl script output? (See the comments for the answer.)


#!/usr/bin/perl

use strict;
use warnings;

my $priority = '0009';
print $priority + 1;
print "\n";

1 Comments:

Blogger md said...

The answer: the program prints the number 10.

Perl is a loosely typed language. The interpreter "figures out" the type of the variable based on the context - when possible.

As another example, this code will produce a warning:

#!/usr/bin/perl

use strict;
use warnings;

my $priority = 'waterlily';
print $priority + 1;
print "\n";


Running the code we get:

username@host:~$ perl mytest.pl
Argument "waterlily" isn't numeric in addition (+) at jnk.pl line 7.
1

2:26 PM  

Post a Comment

<< Home