Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although <a href="https://stackoverflow.com/questions/2791833/how-do-i-implement-a-dispatch-table-in-a-perl-oo-module/2791880#2791880">Robert P</a>'s answer might work for you, it has the problem of fixing the dispatch very early in the process. I tend to resolve the methods as late as I can, so I would leave the things in the <code>tests</code> array as method names until you want to use them:</p> <pre><code> $self-&gt;{tests} = [ qw( _sub1 _sub2 ) ]; </code></pre> <p>The strength of a dynamic language is that you can wait as long as you like to decide what's going to happen.</p> <p>When you want to run them, you can go through the same process that Robert already noted. I'd add an interface to it though:</p> <pre><code> foreach my $method_name ( $obj-&gt;get_test_methods ) { $obj-&gt;$method_name(); } </code></pre> <p>That might even be better as not tying the test to an existing method name:</p> <pre><code> foreach my $method_name ( $obj-&gt;get_test_methods ) { $obj-&gt;run_test_named( $method_name ); } </code></pre> <p>That <code>run_test_named</code> could then be your dispatcher, and it can be very flexible:</p> <pre><code> sub run_test_named { my( $self, $name ) = @_; # do anything you want, like in Robert's answer } </code></pre> <p>Some things you might want to do:</p> <ul> <li>Run a method on an object</li> <li>Pass the object as an argument to something else</li> <li>Temporarily override a test</li> <li>Do nothing</li> <li>etc, etc</li> </ul> <p>When you separate what you decide to do from its implementation, you have a lot more freedom. Not only that, the next time you call the same test name, you can do something different.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload