开发者

How can I add a function based on the version of perl?

开发者 https://www.devze.com 2023-02-18 05:43 出处:网络
I\'m sorry if this had been asked, but I found it hard to search for. I use Perl 5.12 locally but some of our machines use Perl 5.8.8 and they won\'t be updated for the time being.

I'm sorry if this had been asked, but I found it hard to search for.

I use Perl 5.12 locally but some of our machines use Perl 5.8.8 and they won't be updated for the time being.

For auditing I use 'say' on platform 5.12.

I've written a simple function to implement say on 5.8.8 but I don't want to use it on 5.12.

Is there a way to only use my say function on the older version of Perl and use the 'bu开发者_如何学Goiltin' version of say on 5.12?


You can use the $^V special variable 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" }
    }    
}


This should work:

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

精彩评论

暂无评论...
验证码 换一张
取 消