chollida
chollida

Reputation: 7894

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

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 'builtin' version of say on 5.12?

Upvotes: 7

Views: 115

Answers (2)

Brad Gilbert
Brad Gilbert

Reputation: 34120

This should work:

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

Upvotes: 8

Eugene Yarmash
Eugene Yarmash

Reputation: 149813

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" }
    }    
}

Upvotes: 9

Related Questions