<div dir="ltr">This blog post by mst covers this "madness" in some detail: <a href="http://shadow.cat/blog/matt-s-trout/madness-with-methods/">http://shadow.cat/blog/matt-s-trout/madness-with-methods/</a><div><br></div><div>The code blocks seem to be borked cause of the new theme his site is using though...</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Aug 23, 2016 at 5:01 PM Julian Brown via Houston <<a href="mailto:houston@pm.org">houston@pm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I need to know if I am an "old" dog trying to learn new tricks.<div><br></div><div>--------------------------------------</div><div><div><font face="monospace, monospace">$ cat ./<a href="http://doit.pl" target="_blank">doit.pl</a></font></div><div><font face="monospace, monospace">#!/usr/bin/perl</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">use strict;</font></div><div><font face="monospace, monospace">use warnings;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">my $ref = \( "a", "b" );</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">print ":$ref:\n";</font></div><div><font face="monospace, monospace">print ":${$ref}:\n";</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">my $other = ("a", "b");</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">print "other :$other:\n";</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">$ ./<a href="http://doit.pl" target="_blank">doit.pl</a></font></div><div><font face="monospace, monospace">Useless use of a constant (a) in void context at ./<a href="http://doit.pl" target="_blank">doit.pl</a> line 11.</font></div><div><font face="monospace, monospace">:SCALAR(0xaf9998):</font></div><div><font face="monospace, monospace">:b:</font></div><div><font face="monospace, monospace">other :b:</font></div></div><div>--------------------------------------<br></div><div><br><div><span style="font-size:12.8px">This is what confused me \( ).</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I think I am learning a new thing (not a trick).</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The "use warnings" is enlightening.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I never really ever used a list in a scalar context before, in a scalar context it always returns the last item.   The warning is telling me that I am essentially throwing away the value of "a".</span></div></div></div><div dir="ltr"><div><div><br></div><div><span style="font-size:12.8px">Julian</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 23, 2016 at 4:08 PM, Zakariyya Mughal via Houston <span dir="ltr"><<a href="mailto:houston@pm.org" target="_blank">houston@pm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On 2016-08-23 at 15:16:42 -0500, Robert Stone wrote:<br>
> Greetings,<br>
><br>
> Wow, <a href="https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod" rel="noreferrer" target="_blank">https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod</a> is<br>
> a treasure trove of goodies!  Thanks for sharing that.<br>
><br>
> I believe this is the part of the documentation that stands out to me as<br>
> most relevant:<br>
><br>
</span>>         *This is a container, or circumfix operator. The expression inside<br>
<span>> the [] is run in list context, stored in an anonymous array, which is<br>
</span>> immediately dereferenced by @{}.*<br>
<span>><br>
> The "trick" here, as you pointed out, is the ${ } circumfix ScalarRef<br>
> dereference.  However, in order to use it we have to have, unsurprisingly,<br>
> a ScalarRef, hence the need for \( ) to convert the return value of<br>
> $object->foo first into a ScalarRef and then usage of ${ } for both<br>
> dereferencing it back and that tasty circumfixing action (which is higher<br>
> in precedence then the regex-ing, which is why this code snippet works)!<br>
><br>
> Does that sound about right?<br>
<br>
</span>Yep! However, I believe that the `${}` evaluation inside of `m//` is due<br>
to interpolation rather than precedence. In `perldoc perlop`, Perl puts terms<br>
(e.g., quote-like operators and variables) higher than anything else and<br>
these get processed left to right.<br>
<br>
You can also used B::Concise to see how Perl parses the expression:<br>
<br>
  perl -MO=Concise -e 'qr/before\Q${\( $object->foo )}\Eafter/;'<br>
<br>
Really fascinating stuff to play around with!<br>
<div><div><br>
Cheers,<br>
- Zaki Mughal<br>
<br>
><br>
> Best Regards,<br>
> Robert Stone<br>
><br>
> On Tue, Aug 23, 2016 at 2:57 PM, Zakariyya Mughal <<a href="mailto:zaki.mughal@gmail.com" target="_blank">zaki.mughal@gmail.com</a>><br>
> wrote:<br>
><br>
> > 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<br>
> > attribute.<br>
> > > Good call!<br>
> > ><br>
> > > Are you thinking that given the above, the \( ) business makes<br>
> > $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<br>
> > doesn't<br>
> > > see it as regex-y?<br>
> ><br>
> > 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/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/" rel="noreferrer" target="_blank">https://github.com/zmughal/</a><br>
> > vimrc/commit/ad7894dcbe273b0ecb9703db88b51aa9a33d7f0c>.<br>
> ><br>
> >  Cheers,<br>
> >  - Zaki Mughal<br>
> ><br>
> > ><br>
> > > Best Regards,<br>
> > > Robert Stone<br>
> > ><br>
> > > On Tue, Aug 23, 2016 at 2:19 PM, Julian Brown via Houston <<br>
> > <a href="mailto:houston@pm.org" target="_blank">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/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 <<br>
> > <a href="mailto:houston@pm.org" target="_blank">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<br>
> > regular<br>
> > > >> expression every now and then.  Typically I accomplish this via<br>
> > (warning<br>
> > > >> that all examples are very contrived and may contain bugs):<br>
> > > >><br>
> > > >> *Sample Using Variable Assignment First*<br>
> > > >><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<br>
> > directly<br>
> > > >> in regular expressions:<br>
> > > >><br>
> > > >> *MyObject*<br>
> > > >><br>
> > > >> package MyObject;<br>
> > > >><br>
> > > >> use Moose;<br>
> > > >><br>
> > > >> has foo => ( is => 'ro', isa => 'Str' );<br>
> > > >><br>
> > > >> ...;<br>
> > > >><br>
> > > >> *Script That Consumes MyObject*<br>
> > > >><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-" rel="noreferrer" target="_blank">http://perldoc.perl.org/perlre.html#Regular-</a><br>
> > Expressions<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>
> > > >> _______________________________________________<br>
> > > >> Houston mailing list<br>
> > > >> <a href="mailto:Houston@pm.org" target="_blank">Houston@pm.org</a><br>
> > > >> <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/houston</a><br>
> > > >> Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
> > > >><br>
> > > ><br>
> > > ><br>
> > > > _______________________________________________<br>
> > > > Houston mailing list<br>
> > > > <a href="mailto:Houston@pm.org" target="_blank">Houston@pm.org</a><br>
> > > > <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/houston</a><br>
> > > > Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
> > > ><br>
> ><br>
> > > _______________________________________________<br>
> > > Houston mailing list<br>
> > > <a href="mailto:Houston@pm.org" target="_blank">Houston@pm.org</a><br>
> > > <a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/houston</a><br>
> > > Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a><br>
> ><br>
> ><br>
_______________________________________________<br>
Houston mailing list<br>
<a href="mailto:Houston@pm.org" target="_blank">Houston@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/houston</a><br>
Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a></div></div></blockquote></div><br></div>
_______________________________________________<br>
Houston mailing list<br>
<a href="mailto:Houston@pm.org" target="_blank">Houston@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/houston" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/houston</a><br>
Website: <a href="http://houston.pm.org/" rel="noreferrer" target="_blank">http://houston.pm.org/</a></blockquote></div>