Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

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?

like image 819
Hari Krishna Avatar asked May 12 '16 10:05

Hari Krishna


People also ask

For which purpose if statement is used?

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.

Why do we use JavaScript statements?

Statements are used in JavaScript to control its program flow. Unlike properties, methods, and events, which are fundamentally tied to the object that owns them, statements are designed to work independently of any JavaScript object.

What is the purpose of the return statement in a function?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

Can you use or in an if statement?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)


2 Answers

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.

like image 151
Sobrique Avatar answered Oct 17 '22 07:10

Sobrique


say is a feature, it's not (yet - will it ever be?) regular perl syntax.

Use either

use feature qw(say);

or

use v5.010; # or any version later
like image 33
Justin Catterall Avatar answered Oct 17 '22 06:10

Justin Catterall