Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • # Hello Demeter!  (Yeah, this is contrived)
    my $office = $cust->salesman->manager->office;
    What's wrong with this?
    • my $office = $cust->salesman->manager->office;

      It's a question of encapsulation. Imagine that the Salesman class no longer has need of a manager accessor, but you still need to know which office the salesman is with. You might get a office method pushed into the Salesman class. Then, every place where you have the above code needs to be changed to:

      my $office = $cust->salesman->office;

      If that's called a lot, it can be a maintenance nightmare.

      However, what if you have this?

      sub Cus

      • Thank you. I hadn’t realized Moose’s powerful delegation mechanisms we’re more than handy shortcuts. This provides an excellent use case for them.

        For those who don’t know Moose every attribute object can have a handles er … attribute … so the example here can be defined like:

        package Salesman;
        use Moose;

        has manager  => (
            handles => [qw( office )],

        package Cusotomer;
        use Moose;

        has salesman => (
            isa => 'Salesman',
            handles => [qw( office )],

        Moose will setup Salesman::office which delegates to $self->manager->office and Customer::office which will delegate to $self->salesman->office providing exactly the kind of encapsulation wanted here.

        • Thanks for very thoughtful approach to teaching OO. Unfortunately, in my college days we learned OO in a worsed possible way (questionable faculty, clueless students, books never used).