Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Allow me to throw my hat into the ring. I've tested this with Data::FormValidator and HTML::Template and it seems to work fine.

package Object::WithParams; use warnings; use strict; use Carp qw/ croak /; =pod =head1 NAME Object::WithParams - An Object With Params =head1 VERSION Version 0.1 =cut our $VERSION = '0.1'; =head1 SYNOPSIS use Object::WithParams; my $thingy = Object::WithParams->new(); # set a param $thingy->param(veggie => 'tomato'); # get a param my $veggie = $thingy->param('veggie'); # $veggie eq 'tomato' # get all params my @params = $thingy->param(); # @params == ('veggie') # clone a Object::WithParams my $doodad = $thingy->clone; # $doodad->param('veggie') == 'tomato +' # delete a param $thingy->delete('veggie'); # delete all params $thingy->clear(); =head1 DESCRIPTION Use this module to create objects that do nothing except contain param +eters defined by you which you can get and set as you wish. Many modules su +ch as L<Data::FormValidator> have methods that accept an object with a param +() method and this object should be compatible with all of them. This module really ought to be a role but there is no standardized way + to do that in Perl 5. (Not everyone uses L<Moose>.) =head1 METHODS =head2 new Creates a new, empty L<Object::WithParams>. =cut sub new { my ($class) = @_; my $self = {}; return bless $self, $class; } =head2 clear Deletes all the extent parameters. Does not return anything. $thingy->clear(); =cut sub clear { my ($self) = @_; foreach my $param ( keys %{$self} ) { delete $self->{$param}; } return; } =head2 clone Returns a new L<Object::WithParams> with the same set of parameters as + the old one. my $doodad = $thingy->clone(); =cut sub clone { my ($self) = @_; my $clone = Object::WithParams->new(); foreach my $param ( $self->param() ) { $clone->param( $param => $self->param($param) ); } return $clone; } =head2 delete Delete the named parameter. $thingy->delete('veggie'); =cut sub delete { ## no critic 'Subroutines::ProhibitBuiltinHomonyms' my ( $self, $param ) = @_; if ( defined $param && exists $self->{$param} ) { delete $self->{$param}; } return; } =head2 param The C<param> method can be called in three ways. =over 4 =item with no arguments. Returns a list of the parameters contained in the object. my @params = $thingy->param(); =item with a single scalar argument. The value of the parameter with the name of the argument will be retur +ned. my $color = $thingy->param('color'); =item with named arguments A parameter is created for one or more sets of keys and values. $thingy->param(filename => 'logo.jpg', height => 50, width => 100); You could also use a hashref. my $arg_ref = { filename => 'logo.jpg', height => 50, width => 100 }; $thingy->param($arg_ref); The value of a parameter need not be a scalar, it could be any any sor +t of reference even a coderef. $thingy->param(number => &pick_a_random_number); Does not return anything. =back =cut sub param { my ( $self, @args ) = @_; my $num_args = scalar @args; if ($num_args) { if ( ref $args[0] eq 'HASH' ) { # a hashref %{$self} = ( %{$self}, %{ $args[0] } ); } elsif ( $num_args % 2 == 0 ) { # a hash %{$self} = ( %{$self}, @args ); } elsif ( $num_args == 1 ) { # a scalar return $self->{ $args[0] }; } else { croak('Odd number of arguments passed to param().'); } } else { return keys %{$self}; } return; } 1;

What do you think? I can finish writing the tests and put it up on on CPAN if it is of interest.


In reply to Re^3: Ubiquity of the CGI param method by jaldhar
in thread Ubiquity of the CGI param method by jwark

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-04-20 05:39 GMT
    Find Nodes?
      Voting Booth?