[Pdx-pm] oh, gross (object method in regex)

Eric Wilhelm scratchcomputing at gmail.com
Sun Mar 12 15:46:13 PST 2006


# from Randall Hansen
# on Sunday 12 March 2006 01:03 pm:

>this works:
>
>     grep /${ \$object->method }/ => @stuff;
>
>but it looks disgusting.  is there a prettier way to do it?

Is this prettier?

  grep /@{[ $object->method ]}/ => @stuff;

You're dereferencing a newly-created reference to the anonymous scalar 
which is returned by method().

$ perl -MO=Deparse,-p -e '$obj = "main"; sub foo {"thing"}; print 
grep(/${\$obj->foo}/, "a thing", "deal"), "\n"; print "${\$obj->foo} 
(@{[$obj->foo]})\n";print grep(/@{[$obj->foo]}/, "a thing", "deal"), 
"\n";'

($obj = 'main');
sub foo {
    'thing';
}
print(grep(/${\($obj->foo);}/, 'a thing', 'deal'), "\n");
print("${\($obj->foo);} (@{[$obj->foo];})\n");
print(grep(/@{[$obj->foo];}/, 'a thing', 'deal'), "\n");

--Eric
-- 
Like a lot of people, I was mathematically abused as a child.
--Paul Graham
---------------------------------------------------
    http://scratchcomputing.com
---------------------------------------------------


More information about the Pdx-pm-list mailing list