|Mandalika's scratchpad||[ Work blog @Oracle | My Music Compositions ]|
By default new features are not enabled in Perl mainly to retain backward compatibility. Starting with release 5.10, latest features have to be enabled explicitly before using them. Here are a few ways to enable Perl features.
Enable new features with the help of
-Eis similar to -e switch except that
-Ealso enables all latest features. In other words,
-Eswitch on the Perl command line enables the feature bundle for that version of Perl in the main compilation unit. (see #4 below for enabling feature bundles explicitly).
% perl -e 'print $^V;' v5.16.3 % perl -e "print q(Hello)" Hello% ⏎ % perl -E "print q(Hello)" Hello% ⏎ % perl -e "say q(Hello)" syntax error at -e line 1, near "say q(Hello)" Execution of -e aborted due to compilation errors. % perl -E "say q(Hello)" Hello %
Enable one or more features selectively with the help of
use feature pragma.
use featurepragma minimizes the risk of breaking existing programs as the specified feature will only be enabled within the scope of the pragma.
% cat -n enable_say.pl 1 #!/bin/perl 2 say("Hello") % ./enable_say.pl Undefined subroutine &main::say called at ./enable_say.pl line 2. % cat -n enable_say.pl 1 #!/bin/perl 2 use feature say; 3 say("Hello") % ./enable_say.pl Hello
To enable multiple features, spearate each one with spaces. eg.,
use feature qw(say state switch unicode_strings).
Enable all features available in the requested version with the help of
use <VERSION> directive.
use <VERSION>disables any features not in the requested version's feature bundle. (see #4 below for enabling feature bundles explicitly). This is useful to check the current Perl version before using library modules that won't work with older versions of Perl.
% cat -n enable_say.pl 1 #!/bin/perl 2 say("Hello") % ./enable_say.pl Undefined subroutine &main::say called at ./enable_say.pl line 2. % cat -n enable_say.pl 1 #!/bin/perl 2 use v5.10; 3 say("Hello") % ./enable_say.pl Hello
use <VERSION> directive, feature bundles help load multiple features together. Any feature bundle can be enabled with the help of
The colon that prefixes the feature bundle differentiates the bundle from an actual feature.
say, state, switch, unicode_strings, unicode_eval, evalbytes, current_sub and
fc features, enable feature bundle 5.16 as shown below.
#!/bin/perl .. use feature ":5.16"; ..
Perl features available in various feature bundles are listed here.
Credit: various sources