I don't think I'd really recommend it but I think you are trying to do something like this:

#!/usr/bin/perl -w use strict; package Foo; sub new { bless { "_foo", 1, "_bar", 1, "_qux", 1 } } sub runTests { my ($self) = @_; for my $symname (sort keys %{$self}) { local *sym = ref($self)."::$symname"; &sym if defined(&sym) and $symname =~ /^_.*/; } } # sub runTests sub _foo { print "_foo called\n" } sub _bar { print "_bar called\n" } package main; my $foo = Foo->new(); $foo->runTests();

There are a couple of issues with that approach though. One of which is that the subs won't be called as methods at all. They'll be invoked as regular old subroutine calls. The other issue is that it doesn't take inheritance into account. A better approach might be something like:

sub runTests { my ($self) = @_; for my $symname (sort keys %$self) { next unless $symname =~ /^_/; $self->$symname() if $self->can($symname); } }

I think that's somewhat more readable as well. It depends on exactly what behavior you want too.

Update: After seeing some of the other posts it looks like you are actually trying to walk the symbol table of the package the object is blessed into and then to call each of those methods on the object. With that in mind, that second sub would look more like this:

sub runTests3 { my ($self) = @_; no strict 'refs'; for my $symname (sort keys %{ref($self).'::'}) { next unless $symname =~ /^_/; $self->$symname() if $self->can($symname); } }
-sauoq
"My two cents aren't worth a dime.";

In reply to Re: running unknown methods by sauoq
in thread running unknown methods by markcsmith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.