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 2007-01-09 15:27:07-08 by gaetan
:Init attribute and OIO::Args exception
Hello all and happy new year !
In one of my previous mail I sent, I sent the following question:
An object has 2 attributes, each one having a type. Only one of these attributes is mandatory for the object. How can I express it with your module?
Jerry, your response was:
"The easiest way is to use an :Init subroutine:
package Class_Object2; { use Object::InsideOut; my @attribute1 :Field :Arg('name' => 'attribute1') :Type(ClassName1); my @attribute2 :Field :Arg('name' => 'attribute2') :Type(ClassName2); sub init :Init { my ($self, $args) = @_; # Must have at least one of these if (! defined($attribute1[$$self]) && ! defined($attribute2[$$self])) { OIO::Args->die( 'error' => 'Missing mandatory arg', 'Usage' => q/Must specify at least one of 'attribute1' and 'attribute2'/, 'ignore_package' => __PACKAGE__); } } }

But if I create a object Class_Object2 with no parameter, OIO::Code exception is raised instead of OIO::Args (OIO::Args exception is not rethrown in subroutine _args from InsideOut.pm)
Is it possible to retrieve the OIO::Args exception !
Thanks
Direct Responses: 3999 | Write a response
Posted on 2007-01-09 15:45:30-08 by jdhedden in response to 3996
Re: :Init attribute and OIO::Args exception
You must have some other issue. The following:
#!/usr/bin/perl use strict; use warnings; $| = 1; package Class_Object2; { use Object::InsideOut; my @attribute1 :Field :Arg('name' => 'attribute1') :Type(ClassName1); my @attribute2 :Field :Arg('name' => 'attribute2') :Type(ClassName2); sub init :Init { my ($self, $args) = @_; # Must have at least one of these if (! defined($attribute1[$$self]) && ! defined($attribute2[$$self])) { OIO::Args->die( 'error' => 'Missing mandatory arg', 'Usage' => q/Must specify at least one of 'attribute1' and 'attribute2'/, 'ignore_package' => __PACKAGE__); } } } package main; MAIN: { my $obj = Class_Object2->new(); } exit(0); # EOF
Produces:
OIO::Args error: Missing mandatory arg Usage: Must specify at least one of 'attribute1' and 'attribute2' Package: main File: oio.pl Line: 38 Trace begun at oio.pl line 38
The OIO::Args error is throw via line 1512 in the 'new' subroutine, not inside '_args'. Exactly what error message are you getting?
Direct Responses: 4000 | Write a response
Posted on 2007-01-09 16:23:26-08 by gaetan in response to 3999
Re: :Init attribute and OIO::Args exception
Oups,
Sorry, but I mix up problems The previous code is running correctly
This code
package toto; use strict; use warnings; use Object::InsideOut; my @attr1 :Field; my %_init_args :InitArgs = ( 'attr1' => { 'field' => \@attr1, 'regex' => qr/attr1/, 'mandatory' => 1, 'preprocess' => \&toto::_verify_attr1 } ); sub _verify_attr1 { my (undef, undef, undef, $obj, $value) = @_; OIO::Args->die('error' => 'Wrong usage', 'Usage' => 'This usage', 'ignore_package' => __PACKAGE__); return $value; } package main; toto->new('attr1' => 'val1'); 1;
raises the OIO::Code exception.
I was intuitively waiting for an OIO::Args exception.
Would it be possible to modify the subroutine _args from InsideOut.pm so that OIO raised exception are rethrown (instead of raising a OIO::Code exception) ?
Again, sorry for my confusing previous post
Gaetan
Direct Responses: Write a response