[pm-h] Using Moose Attributes In Regular Expressions - Question Regarding Magic ${\( ... )}

Robert Stone drzigman at drzigman.com
Tue Aug 23 12:35:38 PDT 2016


Greetings,

You are correct in the sense that $object->foo actually calls a method
"foo" created via Moose Magic that returns the value of the foo attribute.
Good call!

Are you thinking that given the above, the \( ) business makes $object->foo
a coderef, and then ${ } executes it because it dereferences it?  Given
that, why does this not match:

if( $some_value =~ m/\Q$object->foo\E/ ) {

While this does:

if( $some_value =~ m/\Q${\( $object->foo )}\E/ ) {

Is that ${ } "protecting" the $object->foo so that the regex engine doesn't
see it as regex-y?

Best Regards,
Robert Stone

On Tue, Aug 23, 2016 at 2:19 PM, Julian Brown via Houston <houston at pm.org>
wrote:

> I am not familiar with Moose, but am not convinced this is specific to
> Moose.
>
> I assume $object->foo is really a method call that returns the foo
> attribute?  Or is it like a hash value?
>
>
> • [root at julian64:~/work]# cat doit.pl
> #!/usr/local/cpanel/3rdparty/bin/perl
>
> use strict;
> use warnings;
>
> my $var = "A";
> my $var_ref = \$var;
>
> print "VAR :${var}:\n";
> print "VAR VAR :${$var_ref}:\n";
>
> Output is:
>
> VAR :A:
> VAR VAR :A:
>
> I think we are in the same realm perhaps the parens inside the ${} is
> necessary to execute the method?
>
> Julian
>
>
>
> On Tue, Aug 23, 2016 at 2:02 PM, Robert Stone via Houston <houston at pm.org>
> wrote:
>
>> Greetings,
>>
>> I find myself needing to use the value of a Moose Attribute in a regular
>> expression every now and then.  Typically I accomplish this via (warning
>> that all examples are very contrived and may contain bugs):
>>
>> *Sample Using Variable Assignment First*
>>
>> my $value = $my_moose_object->attribute;
>> if( $some_value =~ m/\Q$value/ ) {
>>
>> Needless to say this isn't the most efficient/easiest to work with.
>> Given that fact, I've come across a way of using Moose Attributes directly
>> in regular expressions:
>>
>> *MyObject*
>>
>> package MyObject;
>>
>> use Moose;
>>
>> has foo => ( is => 'ro', isa => 'Str' );
>>
>> ...;
>>
>> *Script That Consumes MyObject*
>>
>> my $object = MyObject->new( foo => 'Value' );
>> if( $some_value =~ m/\Q${\( $object->foo )}\E/ ) {
>>
>>
>> This works, but frankly I'm not entirely certain why.  From the
>> documentation at http://perldoc.perl.org/perlre.html#Regular-Expressions
>> I have:
>>
>> \Q          quote (disable) pattern metacharacters until \E
>> \E          end either case modification or quoted section, think vi
>>
>> Great, that makes sense.
>>
>> But what magic is the ${\( ... )} doing here?  I'd be most grateful if
>> anyone had some insight and could share it with us!
>>
>> Best Regards,
>> Robert Stone
>>
>>
>>
>> _______________________________________________
>> Houston mailing list
>> Houston at pm.org
>> http://mail.pm.org/mailman/listinfo/houston
>> Website: http://houston.pm.org/
>>
>
>
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/houston/attachments/20160823/61c46c46/attachment-0001.html>


More information about the Houston mailing list