Skip to content

Instantly share code, notes, and snippets.

@Util
Created April 27, 2010 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Util/381431 to your computer and use it in GitHub Desktop.
Save Util/381431 to your computer and use it in GitHub Desktop.
# in http://irclog.perlgeek.de/parrot/2010-04-27#i_2270887 , NotFound said:
# How can I look for a method given a class object? inspec_str('methods')['methodname'] ?
.sub 'main'
.local int ii
.local pmc class, obj
newclass class, 'Foo'
# If all you have is a class, you can use "inspect".
# I think that "inspect_str" has been removed as an op.
$I0 = has_method( class, 'SomeMethod' )
say $I0
$I0 = has_method( class, 'NotAMethod' )
say $I0
# The 'can' op does not work on classes.
# I suspect this is an oversight in the op's design.
$I0 = can class, 'SomeMethod' # Fails!
say $I0
$I0 = can class, 'NotAMethod'
say $I0
# If you can instantiate the class, then "can" is more concise.
new obj, class
$I0 = can obj, 'SomeMethod'
say $I0
$I0 = can obj, 'NotAMethod'
say $I0
# Update: Austin tells me that this works on the uninstantiated class,
# and might also take inheritance into account. I have not verified the latter.
$P0 = class.'find_method'( 'SomeMethod' )
$I0 = defined $P0
say $I0
$P0 = class.'find_method'( 'NotAMethod' )
$I0 = defined $P0
say $I0
.end
.sub has_method
.param pmc class
.param string method_name
.local pmc meths_hash
.local pmc method_code
.local int r_bool
meths_hash = inspect class, 'methods'
method_code = meths_hash[method_name]
r_bool = defined method_code
.begin_return
.set_return r_bool
.end_return
.end
.namespace [ 'Foo' ]
.sub SomeMethod :method
.end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment