Karel Bílek
Karel Bílek

Reputation: 37658

Why doesn't this Moose coercion subroutine work on Perl 5.8.8?

I try to use Moose. It's great, when it works.

However, I have found something that looks like a bug (but I will ask here before committing it).

When I run this code

package Experiment;

use Moose;
use Moose::Util::TypeConstraints;

subtype 'K'
    => as 'Str';

coerce 'K'
    => from 'Str'
    => via {
        s/^([^_]*).*$/$1/;
    };

has 'k' => (
    is => 'ro',
    isa => 'K',
    coerce => 1
);

1;
package main;

my $exp = new Experiment(k=>"abcd_efgh");
print $exp->k."\n";

On one computer with perl 5.010, it correctly shows abcd. When I run it on another computer with 5.8.8 (where the script, unfortunately, has to work too), I see incorrect abcd_efgh; and the coercing subroutine never seem to actually run (if I add print or die there, for example, nothing happens on the 5.8.8 machine).

(It is really simplified, but it is an actual real life case - I need to coerce K from strings, that always have rubbish after underscore)

Do you think it is a Moose bug?

Upvotes: 1

Views: 481

Answers (1)

Ether
Ether

Reputation: 53976

If you are really running perl 5.8.0, it's not a bug, because Moose requires a minimum version of perl 5.8.3. Sorry.

Are you really sure you can't upgrade? 5.8.0 is really really old, and moving to 5.8.8 (which itself is quite old, but is still run in a lot of enterprise environments) will buy you lots of bug fixes.

But (thanks doy for noticing this), your coercion will never run anyway, because the existing type constraint 'K' will pass. You need to add a where clause to the subtype definition, in order to trigger the coercion:

subtype 'K'
    => as 'Str'
    => where { $_ !~ /_/ };

coerce 'K'
    => from 'Str'
    => via {
        m/^([^_]*).*$/; $1;
        # this is an alternate method:
        # (split('_', $_, 2))[0];
    };

(Note that the substitution operator returns the number of substitutions made; I've proposed two alternate rewrites above.)

Lastly, the code as you have written in your question prints "abcd_efgh" on a fully-operational system, so are you sure you are running the same code on both boxes?

Upvotes: 6

Related Questions