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
MooseX::Role::Matcher(3) User Contributed Perl Documentation MooseX::Role::Matcher(3)

MooseX::Role::Matcher - generic object matching based on attributes and methods

version 0.05

  package Person;
  use Moose;
  with 'MooseX::Role::Matcher' => { default_match => 'name' };

  has name  => (is => 'ro', isa => 'Str');
  has age   => (is => 'ro', isa => 'Num');
  has phone => (is => 'ro', isa => 'Str');

  package main;
  my @people = (
      Person->new(name => 'James', age => 22, phone => '555-1914'),
      Person->new(name => 'Jesse', age => 22, phone => '555-6287'),
      Person->new(name => 'Eric',  age => 21, phone => '555-7634'),
  );

  # is James 22?
  $people[0]->match(age => 22);

  # which people are not 22?
  my @not_twenty_two = Person->grep_matches([@people], '!age' => 22);

  # do any of the 22-year-olds have a phone number ending in 4?
  Person->any_match([@people], age => 22, phone => qr/4$/);

  # does everyone's name start with either J or E?
  Person->all_match([@people], name => [qr/^J/, qr/^E/]);

  # find the first person whose name is 4 characters long (using the
  # default_match of name)
  my $four = Person->first_match([@people], sub { length == 4 });

This role adds flexible matching and searching capabilities to your Moose class. It provides a match method, which tests attributes and methods of your object against strings, regexes, or coderefs, and also provides several class methods for using match on lists of objects.

MooseX::Role::Matcher is a parameterized role (see MooseX::Role::Parameterized). The parameters it takes are:
default_match
Which attribute/method to test against by default, if none are specified explicitly. Setting default_match to 'foo' allows using "$obj->match('bar')" rather than "$obj->match(foo => 'bar')".
allow_missing_methods
If set to true, matching against a method that doesn't exist is treated as though matching against undef. Otherwise, the match call dies.

  my $four = Person->first_match([@people], sub { length == 4 });

Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns the first object for which match returns true.

  my @not_twenty_two = Person->grep_matches([@people], '!age' => 22);

Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns the each object for which match returns true.

  Person->any_match([@people], age => 22, number => qr/4$/);

Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns true if any "match" calls return true, otherwise returns false.

  Person->all_match([@people], name => [qr/^J/, qr/^E/]);

Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns false if any "match" calls return false, otherwise returns true.

  $person->match(age => 22);

This method provides the majority of the functionality of this role. It accepts a hash of arguments, with keys being the methods (usually attributes) of the object to be tested, and values being things to test against them. Possible types of values are:

SCALAR
Returns true if the result of the method is equal to ("eq") the value of the scalar, otherwise returns false.
REGEXP
Returns true if the result of the method matches the regexp, otherwise returns false.
CODEREF
Calls the coderef with $_ set to the result of the method, returning true if the coderef returns true, and false otherwise.
UNDEF
Returns true if the method returns undef, or if the object doesn't have a method by this name, otherwise returns false.
ARRAYREF
Matches the result of the method against each element in the arrayref as described above, returning true if any of the submatches return true, and false otherwise.
HASHREF
If the method does not return an object which does MooseX::Role::Matcher, returns false. Otherwise, returns the result of calling "match" on the returned object, with the contents of the hashref as arguments.

Method names can also be given with a leading '!', which inverts that test. The first key can be omitted from the argument list if it is the method name passed to the default_match parameter when composing this role.

  Jesse Luehrs <doy at tozt dot net>

This software is copyright (c) 2008-2009 by Jesse Luehrs.

This is free software; you can redistribute it and/or modify it under the same terms as perl itself.

Better error handling/reporting

Moose

MooseX::Role::Parameterized

No known bugs.

Please report any bugs through RT: email "bug-moosex-role-matcher at rt.cpan.org", or browse to <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Role-Matcher>.

You can find this documentation for this module with the perldoc command.

    perldoc MooseX::Role::Matcher

You can also look for information at:

  • AnnoCPAN: Annotated CPAN documentation

    <http://annocpan.org/dist/MooseX-Role-Matcher>

  • CPAN Ratings

    <http://cpanratings.perl.org/d/MooseX-Role-Matcher>

  • RT: CPAN's request tracker

    <http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Role-Matcher>

  • Search CPAN

    <http://search.cpan.org/dist/MooseX-Role-Matcher>

2009-02-04 perl v5.32.1

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

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