I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2006-10-05 12:40:24-07 by nikc
Argument passing and parsing

This is either a feature request or a "show me the docs I've missed" request. I'm not sure which yet :-)

Does Object::InsideOut provide any support for writing methods that support the same parameter passing style as the constructor?

E.g., where the docs have:

my $obj = My::Class->new('param1' => 'value1'); # or my $obj = My::Class->new({'param1' => 'value1'}); # or even my $obj = My::Class->new( 'param_X' => 'value_X', 'param_Y' => 'value_Y', { 'param_A' => 'value_A', 'param_B' => 'value_B', }, { 'param_Q' => 'value_Q', }, );

I'd like to write my own method that supports the same parameter passing style. I can use similar code to that found in Object::InsideOut::new(), but it would be nice if I could decorate a sub declaration with an attribute that would give me that functionality 'for free', without needing to duplicate code in several places.

Let me know if you'd like me to submit this via RT.

Direct Responses: 3207 | Write a response
Posted on 2006-10-05 16:03:52-07 by jdhedden in response to 3204
Re: Argument passing and parsing
I like this idea. What should the attribute be called? I was thinking about ':StandardArgs' (or :StdArgs for short). You'd use it like:
sub my_method :StdArgs { my ($self, $args) = @_; ... }
where $args would be a hash ref of all the key=>value pairs.
Direct Responses: 3209 | Write a response
Posted on 2006-10-05 17:29:49-07 by nikc in response to 3207
Re: Argument passing and parsing

:StdArgs suggest to me that the argument names are somehow standard. Given that it's the parsing of the arguments that's being standardised, perhaps :StdParse, or :ParseArgs, or :AutoParse, or something like that?

It's occured to me that it would be really nice if the :InitArgs approach could be generalised for multiple subs.

Change :InitArgs to :ArgSpec, since this is now an argument specification hash for multiple subs.

Then, make the top level hash keys sub names (so everything else moves down a level), and use a :ParseArgs attribute on subs to indicate that they should be looked up in this hash. Then you could write something like this.

my %args :ArgSpec = ( 'init' => { 'MY_PARAM' => ... }, 'some_sub' => { 'OTHER_PARAM' => ... }, ); sub init :Init { ... } sub some_sub :ParseArgs { ... } sub other_sub { # parse args by hand ... }

Or, you could keep the arg spec declaration near the sub, by building up the :ArgSpec hash as you go down the file.

my %args :ArgSpec = ( 'init' => { 'MY_PARAM' => ... }, ); sub init :Init { ...} $args{'some_sub'} = { 'OTHER_PARAM' => ... }, ); sub some_sub :ParseArgs { ... }

I've not played much with Perl's attributes, so I don't know how feasible this is.

Of course, they may come a point where it's just esaier to pull in Params::Validate and be done with it.

Direct Responses: 3210 | Write a response
Posted on 2006-10-05 19:03:00-07 by jdhedden in response to 3209
Re: Argument passing and parsing
Maybe :MergeArgs - all the args are merged into a single hash ref.

As to the suggestion to generalize the :InitArgs concept, the code in 'new' that handles :InitArgs is a bit specific. I might be able to generalize it, but then I'd probably be reinventing the wheel. I think it would be best if developers used Params::Validate instead.
Direct Responses: 3666 | Write a response
Posted on 2006-11-30 14:14:02-08 by gaetan in response to 3210
Re: Argument passing and parsing
I read the Param::Validate POD documentation The "problem" with this solution is that the validation process has to be called explicitely at the beginning of the method One solution would be the use of attributes. The attributes implementation could internally use the Param::Validate Advantages of such a solution: . integrated solution (no more explicit call to validation method) . consistent method "prototype" (constructor new and other methods use the same way for the parameters) What do you think about this proposal ? Gaetan
Direct Responses: 3667 | Write a response
Posted on 2006-11-30 15:47:13-08 by jdhedden in response to 3666
Re: Argument passing and parsing
Read the docs for Attribute::Params::Validate which is part of the Params-Validate package. It will do just what you want.

You'll need to wait for the next release of OIO (v2.25), however. I need to make a small change so that you can use Attribute::Params::Validate.

One issue I've noticed so far is that some OIO attributes (e.g., :Restricted) will be incompatible with Attribute::Params::Validate. I'll try to document this in the POD.
Direct Responses: Write a response