Hari Krishna Hari Krishna - 6 months ago 13
Perl Question

Why should I specify a use statement when using the say function?

This question may be stupid. But I just started exploring Perl. I am using Perl v5.16.2. I know that the

say
statement has been introduced in 5.10.

#!/usr/bin/perl

say "Hello World!";


When I try to run above program, I am getting the following error:

$ ./helloPerl
String found where operator expected at ./helloPerl line 3, near "say "Hello World!""
(Do you need to predeclare say?)
syntax error at ./helloPerl line 3, near "say "Hello World!""
Execution of ./helloPerl aborted due to compilation errors.


But when I added the statement
use 5.016;
, it is giving me the correct output.

#!/usr/bin/perl

use 5.016;
say "Hello World!";


My doubt is, I am using perl v5.16.2, which is above 5.010. Why should I mention the Perl version using a
use
statement here?

Answer

Features that might break backwards compatibility, aren't enabled by default.

See perldoc feature:

It is usually impossible to add new syntax to Perl without breaking some existing programs. This pragma provides a way to minimize that risk. New syntactic constructs, or new semantic meanings to older constructs, can be enabled by use feature 'foo' , and will be parsed only when the appropriate feature pragma is in scope. (Nevertheless, the CORE:: prefix provides access to all Perl keywords, regardless of this pragma.)

use on a version number, implicitly enables all features, because it also applies a constraint on perl version. So you won't be tripped over by say not being implemented, for example.

Comments