Perl error message "Do you need to predeclare say?"

In order to bring the string to the screen in the Perl programming language has a built-in function print. But in addition to print and still have say.

Use say in scripts

If you just try to use say in the Perl script, it will error. Here's an example script with the error:

▶ Run
#!/usr/bin/perl

say 'Hello';

If you save this code to a file script.pl and perform perl script.pl, the screen appears error:

String found where operator expected at script.pl line 3, near "say 'Hello'"
    (Do you need to predeclare say?)
syntax error at script.pl line 3, near "say 'Hello'"
Execution of script.pl aborted due to compilation errors.

In order to still take advantage of the say you should explicitly say that we want to use this feature of the language using use feature qw(say);. Here is the full text of the script which works:

▶ Run
#!/usr/bin/perl

use feature qw(say);

say 'Hello';

The result of the script — on screen appears the text Hello.

Use say in odnotrahniki

Some tasks are very convenient to solve using Perl odnotrahniki when the whole text of the program are not in the file and passed the parameter value -e on the command line.

But if you just run perl -e 'say "Hello"', the screen appears error:

String found where operator expected at -e line 1, near "say "Hello""
    (Do you need to predeclare say?)
syntax error at -e line 1, near "say "Hello""
Execution of -e aborted due to compilation errors.

In order to use say in odnotrahniki you can use the same method as for the scripts: write at the command prompt perl -e 'use feature qw(say); say "Hello"'.

But it is much more convenient to use the parameter -E is -e. Option -E includes all the features of the language, so the launch perl -E 'say "Hello"' displays the expected line Hello.

Other articles

Comments