These are a few idioms I had to change in a series of modules (that already worked in 5.6.x and 5.8.x) in order to make them backwards compatible with Perl 5.005_03.

I hope this can be useful to other module authors that are willing to widen their audience:

Perl 5.6 and 5.8Perl 5.005_03See Also
our $var;use vars qw( $var );48350
use warnings; #!perl -w

doesn't work inside modules

282957, 278796, 294493
$func_ref[3]($var); $func_ref[3]->($var);

reference to an array of aliases:

$param_ref = \@_;

no equivalence. In 5.005_03 it makes a copy, just like:

$param_ref = [ @_ ];
289445
$_++ for values %hash; $hash{$_}++ for keys %hash;

or:

$_++ for @hash{keys %hash};
use bytes;
$INC{ 'bytes.pm' }++ if $] < 5.006; use bytes;
294493
open my $fh, "<$filename" or die "Cannot read $filename: $!";
my $fh = do { local *FH; *FH }; open $fh, "<$filename" or die "Cannot read $filename: $!";
DateTime
$object->$method; $object->$method();413389

As ajdelore puts it, "this is not because I think that people should continue to use perl 5.005, but simply in acknowledgement of the fact that many people do use perl 5.005".

See also what PodMaster writes on using use VERSION; in "Code should be version-aware".

Update: fixed the "func_ref" bug. Thanks Merlin, autarch; added a reference to Juerd note.

Update: added use bytes.

Update: added open $fh from autarch DateTime code.

Update: be aware that the order of the keys in a hash may change, so you'd better sort the keys if you want repeatable results between versions.

Update (Dec/2004): Added "$_++ for @hash{keys %hash}", seen in CB, from Merlin. Also added $object->$method() and explained better what happens in the $param_ref example.

Replies are listed 'Best First'.
•Re: Migrating scripts back to Perl 5.005_03
by merlyn (Sage) on Sep 05, 2003 at 23:45 UTC
    As for the code ref dereferencing, I think what you're getting at is the difference between:
    $some_array[3](@args); # 5.6 only
    and
    $somearray[3]->(@args); # 5.5 and 5.4
    Not quite what you have. 5.6 permits the arrow to be dropped for coderefs using the same rules as dropping the arrow for arrayrefs and hashrefs. You still can't drop the arrow at the head of a subscripty chain, now or then, as your example would suggest.

    The coderef arrow notation itself was introduced in 5.4, on a bet. I think I've recounted that story here before somewhere.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Migrating scripts back to Perl 5.005_03
by Juerd (Abbot) on Sep 06, 2003 at 10:22 UTC

    $param_ref = \@_;    $param_ref = [ @_ ];

    Note that in your "5.6" version, you have a reference to an array of aliases, while in your "5.005_03" version, all elements are copied. This matters when you try to mutate $param_ref->[$n]. See the source of Attribute::Property for evil stuff involving referencing @_ :) (Don't worry, it's very well tested).

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Migrating scripts back to Perl 5.005_03
by diotalevi (Canon) on Sep 05, 2003 at 21:16 UTC

    I thought that '-w' would set $^W globally? I upgraded a few of my modules recently so they pass tests on 5.005_03 and I just left warnings off but had them on (and fatal) during tests. If I put a shebang with -w in my module I won't stomp on the calling script or other modules?

      If I understand perllexwarn, which I am not sure that I do, -w does set $^W globally. (And -W makes it irrevocable). Of course, you can't use the shebang in a module.

      While in 5.6+ you could put use warnings; in a module and have it scoped to that file only, I think that setting $^W would be global unless enclosed in a block, or unless it was somehow contained.

      package foo; use strict; $^W = 1; print "Testing\n", undef; 1; __END__ use strict; use foo; print "Testing\n", undef;

      In the above example, both print statements throw a warning. However, if I do:

      local $^W = 1;

      Then I only get one in the module. Of course, the main thing that perllexwarn seems to say is "don't do any of this stuff." It seems better to take warnings as an all-or-nothing deal in 5.005.

      </ajdelore>

      I use #!perl -w only in the test suite.
      This way, users can decide whether to enable warnings or not.

      Update: as I understand from perlrun, a shebang line in a module would not be interpreted at all.

Re: Migrating scripts back to Perl 5.005_03
by Abigail-II (Bishop) on Sep 05, 2003 at 22:03 UTC
    $func_ref($var);

    How is that supposed to work in 5.6? Whatever I try, all I get is a syntax error.

    Abigail

      He's a little off there. I think he meant this: $ref->{key}($var) That does work in 5.6+, but not 5.00503. Doh, I hadn't read merlyn's reply before I wrote this!
Re: Migrating scripts back to Perl 5.005_03
by simonm (Vicar) on Sep 05, 2003 at 22:40 UTC
    The third case needs a bit of revision:
    Perl 5.6Perl 5.005_03
    $func_ref->($var)&$func_ref($var)
      No, the $func_ref->($var) syntax definitely works in 5.00503.