Hari Krishna Hari Krishna - 2 years ago 77
Perl Question

Perl: Why should i specify use statement, while using say function?

This question may be stupid. But I just started exploring Perl. I am using perl v5.16.2. I know "say" statement is introduced in 5.10.


say "Hello World!";

When i try to run above program, I am getting 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 output.


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 use statement here?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download