GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  MOOSE::COOKBOOK::EXTENDING::DEBUGGING_BASECLASSROLE (3)

.ds Aq ’

NAME

Moose::Cookbook::Extending::Debugging_BaseClassRole - Providing a role for the base object class

CONTENTS

VERSION

version 2.1605

SYNOPSIS



  package MooseX::Debugging;

  use Moose::Exporter;

  Moose::Exporter->setup_import_methods(
      base_class_roles => [MooseX::Debugging::Role::Object],
  );

  package MooseX::Debugging::Role::Object;

  use Moose::Role;

  sub BUILD {}
  after BUILD => sub {
      my $self = shift;

      warn "Made a new " . ( ref $self ) . " object\n";
  };



DESCRIPTION

In this example, we provide a role for the base object class that adds some simple debugging output. Every time an object is created, it spits out a warning saying what type of object it was.

Obviously, a real debugging role would do something more interesting, but this recipe is all about how we apply that role.

In this case, with the combination of Moose::Exporter and Moose::Util::MetaRole, we ensure that when a module does use MooseX::Debugging, it automatically gets the debugging role applied to its base object class.

There are a few pieces of code worth looking at more closely.



  Moose::Exporter->setup_import_methods(
      base_class_roles => [MooseX::Debugging::Role::Object],
  );



This creates an import method in the MooseX::Debugging package. Since we are not actually exporting anything, we do not pass setup_import_methods any parameters related to exports, but we need to have an import method to ensure that our init_meta method is called. The init_meta is created by setup_import_methods for us, since we passed the base_class_roles parameter. The generated init_meta will in turn call Moose::Util::MetaRole::apply_base_class_roles.



  sub BUILD {}
  after BUILD => sub {
      ...
  };



Due to the way role composition currently works, if the class that a role is composed into contains a BUILD method, then that will override the BUILD method in any roles it composes, which is typically not what you want. Using a method modifier on BUILD avoids this issue, since method modifiers compose together rather than being overridden. Method modifiers require that a method exists in order to wrap, however, so we also provide a stub method to wrap if no BUILD method exists in the class.

AUTHORS

o Stevan Little <stevan.little@iinteractive.com>
o Dave Rolsky <autarch@urth.org>
o Jesse Luehrs <doy@tozt.net>
o Shawn M Moore <code@sartak.org>
o XXXX XXXXX (Yuval Kogman) <nothingmuch@woobling.org>
o Karen Etheridge <ether@cpan.org>
o Florian Ragwitz <rafl@debian.org>
o Hans Dieter Pearcey <hdp@weftsoar.net>
o Chris Prather <chris@prather.org>
o Matt S Trout <mst@shadowcat.co.uk>

COPYRIGHT AND LICENSE

This software is copyright (c) 2006 by Infinity Interactive, Inc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 MOOSE::COOKBOOK::EXTENDING::DEBUGGING_BASECLASSROLE (3) 2016-02-16

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.