<div dir="ltr">Greetings,<div><br></div><div>Wow, <a href="https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod">https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod</a> is a treasure trove of goodies!  Thanks for sharing that.</div><div><br></div><div>I believe this is the part of the documentation that stands out to me as most relevant:</div><div><br></div><div>        <i>This is a container, or circumfix operator. The expression inside the [] is run in list context, stored in an anonymous array, which is immediately dereferenced by @{}.</i><br></div><div><br></div><div>The "trick" here, as you pointed out, is the ${ } circumfix ScalarRef dereference.  However, in order to use it we have to have, unsurprisingly, a ScalarRef, hence the need for \( ) to convert the return value of $object->foo first into a ScalarRef and then usage of ${ } for both dereferencing it back and that tasty circumfixing action (which is higher in precedence then the regex-ing, which is why this code snippet works)!</div><div><br></div><div>Does that sound about right?</div><div><br></div><div>Best Regards,</div><div>Robert Stone</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 23, 2016 at 2:57 PM, Zakariyya Mughal <span dir="ltr"><<a href="mailto:zaki.mughal@gmail.com" target="_blank">zaki.mughal@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2016-08-23 at 14:35:38 -0500, Robert Stone via Houston wrote:<br>
> Greetings,<br>
><br>
> You are correct in the sense that $object->foo actually calls a method<br>
> "foo" created via Moose Magic that returns the value of the foo attribute.<br>
> Good call!<br>
><br>
> Are you thinking that given the above, the \( ) business makes $object->foo<br>
> a coderef, and then ${ } executes it because it dereferences it?  Given<br>
> that, why does this not match:<br>
><br>
> if( $some_value =~ m/\Q$object->foo\E/ ) {<br>
><br>
> While this does:<br>
><br>
> if( $some_value =~ m/\Q${\( $object->foo )}\E/ ) {<br>
><br>
> Is that ${ } "protecting" the $object->foo so that the regex engine doesn't<br>
> see it as regex-y?<br>
<br>
</span>Not a coderef, but a scalar ref. It is similar to the approach used by<br>
the baby cart operator: <<a href="https://metacpan.org/pod/perlsecret#Baby-cart" rel="noreferrer" target="_blank">https://metacpan.org/pod/<wbr>perlsecret#Baby-cart</a>>.<br>
<br>
  @{[ ]}<br>
<br>
Reading inside out:<br>
<br>
 - [ ... ]  : turn into an ArrayRef<br>
 - @{ ... } : circumfix ArrayRef dereference<br>
<br>
So that approach for the regexp does:<br>
<br>
 - \ ... : turn into a reference (unary \ operator)<br>
 - ${ ... } : circumfix ScalarRef dereference<br>
<br>
I use the baby cart operator enough that I have added a line in my vimrc<br>
to make it work with surround.vim: <<a href="https://github.com/zmughal/vimrc/commit/ad7894dcbe273b0ecb9703db88b51aa9a33d7f0c" rel="noreferrer" target="_blank">https://github.com/zmughal/<wbr>vimrc/commit/<wbr>ad7894dcbe273b0ecb9703db88b51a<wbr>a9a33d7f0c</a>>.<br>
<br>
 Cheers,<br>
 - Zaki Mughal<br>
<div><div class="h5"><br>
><br>
> Best Regards,<br>
> Robert Stone<br>
><br>
> On Tue, Aug 23, 2016 at 2:19 PM, Julian Brown via Houston <<a href="mailto:houston@pm.org">houston@pm.org</a>><br>
> wrote:<br>
><br>
> > I am not familiar with Moose, but am not convinced this is specific to<br>
> > Moose.<br>
> ><br>
> > I assume $object->foo is really a method call that returns the foo<br>
> > attribute?  Or is it like a hash value?<br>
> ><br>
> ><br>
> > • [root@julian64:~/work]# cat <a href="http://doit.pl" rel="noreferrer" target="_blank">doit.pl</a><br>
> > #!/usr/local/cpanel/3rdparty/<wbr>bin/perl<br>
> ><br>
> > use strict;<br>
> > use warnings;<br>
> ><br>
> > my $var = "A";<br>
> > my $var_ref = \$var;<br>
> ><br>
> > print "VAR :${var}:\n";<br>
> > print "VAR VAR :${$var_ref}:\n";<br>
> ><br>
> > Output is:<br>
> ><br>
> > VAR :A:<br>
> > VAR VAR :A:<br>
> ><br>
> > I think we are in the same realm perhaps the parens inside the ${} is<br>
> > necessary to execute the method?<br>
> ><br>
> > Julian<br>
> ><br>
> ><br>
> ><br>
> > On Tue, Aug 23, 2016 at 2:02 PM, Robert Stone via Houston <<a href="mailto:houston@pm.org">houston@pm.org</a>><br>
> > wrote:<br>
> ><br>
> >> Greetings,<br>
> >><br>
> >> I find myself needing to use the value of a Moose Attribute in a regular<br>
> >> expression every now and then.  Typically I accomplish this via (warning<br>
> >> that all examples are very contrived and may contain bugs):<br>
> >><br>
</div></div>> >> *Sample Using Variable Assignment First*<br>
<span class="">> >><br>
> >> my $value = $my_moose_object->attribute;<br>
> >> if( $some_value =~ m/\Q$value/ ) {<br>
> >><br>
> >> Needless to say this isn't the most efficient/easiest to work with.<br>
> >> Given that fact, I've come across a way of using Moose Attributes directly<br>
> >> in regular expressions:<br>
> >><br>
</span>> >> *MyObject*<br>
<span class="">> >><br>
> >> package MyObject;<br>
> >><br>
> >> use Moose;<br>
> >><br>
> >> has foo => ( is => 'ro', isa => 'Str' );<br>
> >><br>
> >> ...;<br>
> >><br>
</span>> >> *Script That Consumes MyObject*<br>
<div class="HOEnZb"><div class="h5">> >><br>
> >> my $object = MyObject->new( foo => 'Value' );<br>
> >> if( $some_value =~ m/\Q${\( $object->foo )}\E/ ) {<br>
> >><br>
> >><br>
> >> This works, but frankly I'm not entirely certain why.  From the<br>
> >> documentation at <a href="http://perldoc.perl.org/perlre.html#Regular-Expressions" rel="noreferrer" target="_blank">http://perldoc.perl.org/<wbr>perlre.html#Regular-<wbr>Expressions</a><br>
> >> I have:<br>
> >><br>
> >> \Q          quote (disable) pattern metacharacters until \E<br>
> >> \E          end either case modification or quoted section, think vi<br>
> >><br>
> >> Great, that makes sense.<br>
> >><br>
> >> But what magic is the ${\( ... )} doing here?  I'd be most grateful if<br>
> >> anyone had some insight and could share it with us!<br>
> >><br>
> >> Best Regards,<br>
> >> Robert Stone<br>
> >><br>
> >><br>
> >><br>
> >> ______________________________<wbr>_________________<br>
> >> Houston mailing list<br>
> >> <a href="mailto:Houston@pm.org">Houston@pm.org</a><br>
> >> <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/<wbr>listinfo/houston</a><br>
> >> Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
> >><br>
> ><br>
> ><br>
> > ______________________________<wbr>_________________<br>
> > Houston mailing list<br>
> > <a href="mailto:Houston@pm.org">Houston@pm.org</a><br>
> > <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/<wbr>listinfo/houston</a><br>
> > Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
> ><br>
<br>
> ______________________________<wbr>_________________<br>
> Houston mailing list<br>
> <a href="mailto:Houston@pm.org">Houston@pm.org</a><br>
> <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/<wbr>listinfo/houston</a><br>
> Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
<br>
</div></div></blockquote></div><br></div>