Params::Coerce

Params::Coerce is a Perl module that allows your classes to do coercion of parameters.
Download

Params::Coerce Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Adam Kennedy
  • Publisher web site:
  • http://search.cpan.org/~adamk/

Params::Coerce Tags


Params::Coerce Description

Params::Coerce is a Perl module that allows your classes to do coercion of parameters. Params::Coerce is a Perl module that allows your classes to do coercion of parameters.SYNOPSIS # Coerce a object of class Foo to a Bar my $bar = Params::Coerce::coerce('Bar', $Foo) # Create a coercion param function use Params::Coerce '_Bar' => 'Bar'; my $bar = _Bar($Foo); # Usage when Bar has a 'from' method my $bar = Bar->from($Foo);Real world example using HTML::Location. # My class needs a URI package Web::Spider; use URI; use Params::Coerce 'coerce'; sub new { my $class = shift; # Where do we start spidering my $start = coerce('URI', shift) or die "Wasn't passed a URI"; bless { root => $start }, $class; } ############################################# # Now we can do the following # Pass a URI as normal my $URI = URI->new('http://ali.as/'); my $Spider1 = Web::Spider->new( $URI ); # We can also pass anything that can be coerced into being a URI my $Website = HTML::Location->new( '/home/adam/public_html', 'http://ali.as' ); my $Spider2 = Web::Spider->new( $Website );A big part of good API design is that we should be able to be flexible in the ways that we take parameters.Requirements:· PerlParams::Coerce attempts to encourage this, by making it easier to take a variety of different arguments, while adding negligable additional complexity to your code.


Params::Coerce Related Software