in reply to a MooseX Q that's driving me up the wall :)

I don't recommend using the number globals in the first place. When I replace $1 with $foo in the code, and then add my ($foo) = in front of each occurance of $num =~, the problem goes away.

It's almost as if MooseX::Declare is doing something magical with $1, and it's somehow aliased until that point, which is why it apparently shows the right value in the call stack until it's replaced by the string "coerce".

Replies are listed 'Best First'.
Re^2: a MooseX Q that's driving me up the wall :)
by tj_thompson (Monk) on Dec 03, 2010 at 08:20 UTC

    I find it somewhat scary that MooseX seems to be messing with some pretty standard Perl magic. I don't think I've ever seen odd nonstandard behavior from regex captures like that before.

    I did notice that the value suddenly shifted to the 'coerce' string. In my more complicated code this is pulled from, I actually saw 'is_subtype_of'. So there's definitely something strange going on in the background. I'm hoping a guru can shed some light on it. I don't like wierd voodo stuff going on that I can't figure out.

    When you say that you don't recommend the numbered capture globals, do you mean in general or specifically when dealing with MooseX? Do you always use your own variables for regex captures? Why? Not being critical, I just like learning and I'm still firmly an early intermediate Perl programmer (by my own guesstimate).

      When you say that you don't recommend the numbered capture globals, do you mean in general or specifically when dealing with MooseX? Do you always use your own variables for regex captures? Why? Not being critical, I just like learning and I'm still firmly an early intermediate Perl programmer (by my own guesstimate).
      In general. I always store the return value of the matched regex (usually in lexical variables), which eliminates a lot of confusion: you know in what scope the variable storing the captured value is, unlike with the globals, and you can do more regex matching without worrying about overwriting previous values. Also, it's simply unnecessary to do
      $string =~ /$pattern/; my $match_foo = $1; my $match_bar = $2;
      when you can do

      my ($match_foo, $match_bar) = $string =~ /$pattern/;

      The relevant programming patterns I use the most probably look something like like

      if (my ($foo, $bar) = $input =~ /$pattern/) {

      and

      my @matches = grep { condition } map { /(foo)(bar)/ } @data;

        I like it...I was never a big fan of the capture variables. I'll probably lift your patterns and tweak 'em a bit. Thanks!