Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This sounds like something for which code references would be more suitable.

After reading up on the Visitor pattern — I wasn't familiar with it by name — I dare say it *is* something for which code references would be more suitable. The example on that paged could be implemented as follows in Perl:

use strict; use warnings; { package Wheel; sub new { my ($class, $name) = @_; return bless({ name => $name }, $class); } sub get_name { my ($self) = @_; return $self->{name}; } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Engine; sub new { my ($class) = @_; return bless({}, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Body; sub new { my ($class) = @_; return bless({}, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); } } { package Car; sub new { my ($class) = @_; return bless({ engine => Engine->new(), body => Body->new(), wheels => [ map { Wheel->new($_) } 'front left', 'front right', 'back left', 'back right', ], }, $class); } sub accept { my ($self, $visitor) = @_; $visitor->($self); $self->{engine}->accept($visitor); $self->{body}->accept($visitor); $_->accept($visitor) foreach @{$self->{wheels}}; } } sub visitor { my ($o) = @_; if ($o->isa('Wheel')) { print("Visiting ", $o->get_name(), " wheel\n"); } elsif ($o->isa('Body')) { print("Visiting body\n"); } elsif ($o->isa('Engine')) { print("Visiting engine\n"); } elsif ($o->isa('Car')) { print("Visiting car\n"); } else { die("Unknown class " . ref($o)); } } { my $car = Car->new(); $car->accept(\&visitor); }

In reply to Re^3: adding temporary method to a class by ikegami
in thread adding temporary method to a class by jeteve

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found