How to add a function based on perl version?

Sorry if this was asked, but it was hard for me to find.

I use Perl 5.12 locally, but some of our machines use Perl 5.8.8, and so far they will not be updated.

For audit, I use "say" on platform 5.12.

I wrote a simple function to implement, say, 5.8.8, but I do not want to use it on 5.12.

Is there a way to use my utterance function in the old version of Perl and use the built-in version like 5.12?

+7
source share
2 answers

You can use the special variable $^V to determine the version of the Perl interpreter:

 BEGIN { if ($^V ge v5.10.1) { # "say" first appeared in 5.10 require feature; feature->import('say'); } else { *say = sub { print @_, "\n" } } } 
+9
source

This should work:

 BEGIN{ no warnings 'once'; unless( eval{ require feature; feature->import('say'); 1 } ){ *say = sub{ print @_, "\n"; } } } 
+8
source

All Articles