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

sub f { 'xxx' =~ /(.*)/; print("$_[0]\n"); } 'aaa' =~ /(.*)/; f($1); # xxx 'aaa' =~ /(.*)/; f("$1"); # aaa

Perl passes arguments by reference, not by value. You are passing a global variable, and the global variable gets changed the by the called function before the argument is used. Passing a copy of the global avoids the problem.

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 17:06 UTC
    That makes sense. And quoting the global like "$1" would result in a literal string being passed, taking the global out of the result. Thanks!
      A string literal is a piece of code, so it's not a string literal being passed. The string literal, however, does create a new string. That's what's being passed.
        Good point on the string. Thank you very much for the insight, I feel far better now that I understand what was going on. Upon introspection, it should have been fairly obvious now that I think about it.
Re^2: a MooseX Q that's driving me up the wall :)
by Anonymous Monk on Dec 03, 2010 at 17:11 UTC
    Huh, I thought that MooseX::Declare just copied to the method's args. But it aliases them instead, like they're aliased in @_? That's pretty cool.