All entries

ABC

06 Nov 2013
Эта же задача по-русски: здесь.

What does this script print?

#!/usr/bin/perl

use strict;

$a = 1;
$b = 2;
$c = 3;

print $a+$b+$c, "\n";

Hint

Show

Notice the use strict statement.

Hint 2

Show

It’s definetely not good to use variables without declaring them.

Hint 3

Show

There are 3 undeclared variables in the script. Of course, it doesn’t work! But where exactly does it fail?

Hint 4

Show

>perl abc.pl
Global symbol "$c" requires explicit package name at abc.pl line 7.
Global symbol "$c" requires explicit package name at abc.pl line 9.
Execution of abc.pl aborted due to compilation errors.

Solution

Show

$a and $b are always declared global variables intended for use in sort function. They always present in any package and don’t break compilation even when use strict is in effect. So the first undeclared variable is in fact $c.

Resume: always do use strict, declare your variables with my or our, otherwise you’ll be sorry ;) And using $a and $b outside of comparison function for sort is a bad idea.