Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Howto "use" backward compability packages of new "feature"s

by ikegami (Patriarch)
on Aug 20, 2010 at 13:36 UTC ( [id://856273]=note: print w/replies, xml ) Need Help??


in reply to Howto "use" backward compability packages of new "feature"s

BEGIN { if( $] < 5.012 ){ require Enumerators; Enumerators->import(); } else { require feature; feature->import(":5.12"); } }

You could use eval to use use for Enumerators, but not for a pragma.

You mentioned keys, but none of the features affect keys. In fact, the only feature you could implement as a module would be say, and there's no reason you couldn't always use your own say instead of this conditional mumbo jumbo.

Missing "0" in the version and missing ":" in the feature, btw.

Replies are listed 'Best First'.
Re^2: Howto "use" backward compability packages of new "feature"s
by LanX (Saint) on Aug 20, 2010 at 21:25 UTC
    Thanks, unfortunately thats what I expected.

    Prepending scripts with nine lines of code is not what I was hoping for.

    (More something like a tricky modul which catches the "Fatal"-exception from the feature pragma and automatically overrides the CORE functions)

    > You mentioned keys, but none of the features affect keys.

    hmm ... apparently it only affects each.

    I didn't check it, just believed what I was told in the post I linked too.

    Cheers Rolf

      What I said about keys applies to values and each as well. It's not a backward incompatible change, so no feature needs to be activated. Your code boils down to

      use if $] < 5.012, 'Enumerators';

      Prepending scripts with nine lines of code is not what I was hoping for.

      That's because you're trying to write your code inside out. A less messy approach would be to include Enumerators unconditionally, and let it decide if it needs to override each or not. It's not a detail the module's user should worry about.

      # use Enumerators -global; # use Enumerators qw( each ); # use Enumerators; package Enumerators; use Exporter qw( ); our @EXPORT = qw( each ); our @EXPORT_OK = qw( each ); sub import { my $class = shift; return if $[ >= 5.012; if ($_[0] eq '-global') { *CORE::GLOBAL::each = \&each; } else { goto &Exporter::import; } } sub each(\[@%]) { ... } 1;

      (Trim out what you don't want.)

      Note that this approach works for pragams too.

      package stricter; sub import { require strict; strict->import; require warnings; warnings->import; } 1;

      use stricter; does the same as use strict; use warnings;

        Thanks a lot for the "stricter" example, the possibility to import pragmas is owesome.

        Do you know where this is documented?

        BTW: just found an "if pragma" which could be used for backwards compatibility!

          use if $] < 5.008, "utf8";

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://856273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-23 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found