Re: Maybe[Foo] type coercions

2010-02-15 Thread mikhail maluyk
Here is slightly less verbose version

package Obj;
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
use DateTime::Format::MySQL;

class_type 'DateTime';

subtype 'MaybeDateTime' => as 'Maybe[DateTime]';

coerce 'MaybeDateTime'
   => from 'Str'
   => via {
   # check for retarded mysql 4.x null times
   return if $_ eq '-00-00 00:00:00';
   return DateTime::Format::MySQL->parse_datetime($_);
   };

coerce 'DateTime'
   => from 'Str'
   => via {
   return DateTime::Format::MySQL->parse_datetime($_)
   };

has 'date' => ( isa => 'MaybeDateTime', is => 'rw', coerce => 1 );

package main;

my $a = Obj->new(date => DateTime->new(year => 2000)); # DT
my $b = Obj->new(date => '-00-00 00:00:00'); # undef
my $c = Obj->new(date => '2000-10-10 00:00:00'); # DT

On Tue, Feb 16, 2010 at 5:25 AM, Karen Etheridge  wrote:
>
> I'm having difficulty getting a type coercion to work that involves Maybes.
> I've looked at the "deep coercion" section of Moose::Manual::Types, and I'm
> not sure what I'm missing to get this to work?
>
> e.g. I'm running
>
> perl -MData::Dumper -MObject -MDateTime -I. -wle'my 
> $o=Object->new(date=>"-00-00 00:00:00"); print Dumper($o)'
>
> against this module, Object.pm:
>
> package Object;
> use Moose;
> use Moose::Util::TypeConstraints;
> use DateTime;
> use DateTime::Format::MySQL;
>
> has date => ( is => 'rw', isa => 'MaybeDateTime', coerce => 1 );
>
> # this generates "Attempt to free unreferenced scalar" error!
> #has date => ( is => 'rw', isa => 'Undef | DateTime', coerce => 1 );
>
> subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };
>
> subtype 'MaybeDateTime'
>    => as 'Maybe[Object]'
>    => where { $_->isa('DateTime') or not defined $_ };
>
> coerce 'MaybeDateTime'
>    => from 'DateTime'
>    => via { return $_ };
>
> coerce 'MaybeDateTime'
>    => from 'Str'
>    => via {
>        print "### calling Str->MaybeDateTime coercion\n";
>        # check for retarded mysql 4.x null times
>        return if $_ eq '-00-00 00:00:00';
>        return DateTime::Format::MySQL->parse_datetime($_);
>    };
>
> coerce 'DateTime'
>    => from 'Str'
>    => via {
>        print "### coercing Str $_ into DateTime\n";
>        return DateTime::Format::MySQL->parse_datetime($_);
>    };
>
> 1;
>
> --
>                      A dozen, a gross, and a score,
>                      plus three times the square root of four,
>                      divided by seven,
>                      plus five times eleven,
>                      equals nine squared and no more!
>            .             .            .            .             .
> Karen Etheridge, ka...@etheridge.ca       GCS C+++$ USL+++$ P+++$ w--- M++
> http://etheridge.ca/                      PS++ PE-- b++ DI e++ h(-)
>



-- 
Regards,
Mikhail


Re: MooseX::App:Cmd question

2009-12-15 Thread mikhail maluyk
Hello, i'm not sure why that is happening with your classes, can you provide
us with some code?

Here is a little code snippet which does the same as you described and works
just fine. Hope that helps.

package MyApp;
use Moose;

extends 'MooseX::App::Cmd';

package MyApp::Command;
use Moose;

has 'bam' => ( isa => 'Str', is => 'rw', required => 1 );

package MyApp::Command::foo;
use Moose;

extends 'MyApp::Command', 'MooseX::App::Cmd::Command';

has 'foo' => ( isa => 'Str', is => 'rw', required => 1 );

package MyApp::Command::bar;
use Moose;

extends 'MyApp::Command', 'MooseX::App::Cmd::Command';

has 'bar' => ( isa => 'Str', is => 'rw', required => 1 );

package main;

MyApp->new->run;

On Tue, Dec 15, 2009 at 5:28 PM, Rolf Schaufelberger  wrote:

> Hi,
>
> how can  I  implement  several commands (= subclasses ) and each subclass
> have its own parameters ?
>
> Say I have:
> MyApp::Command Base classdefines param   B1  and B2 as required
> MyApp::Command::CmdA   defines param  A1 as required
> MyApp:Command::CmdX defines param X1  as required
>
> and my script site-config.pl:
> #!/usr/bin/perl
> use MyApp::Comand -run
>
>
> now when I call
> site-config.pl   CmdA   -B1=bla -B2=foo -A1=bar
>
> I get an error complaining about  missing mandatory parameter  X1
>
> but X1 is not needed to execute CmdA
>
> regards
> Rolf Schaufelberger
>
>
>
>
>


-- 
Regards,
Mikhail


Re: Inherited attribute bug?

2009-06-03 Thread mikhail maluyk
Hi,

"has" doesn't create an accessor for an attribute. "is" or "accessor" does.

On Wed, Jun 3, 2009 at 8:00 PM, Ovid  wrote:

>
> Could someone please explain to me what I'm doing wrong here?
>
>#!/usr/bin/env perl
>
>{
>package My::Base;
>use Moose;
>has some_method => ( default => 'foo' );
>}
>{
>package My::Class;
>use Moose;
>extends 'My::Base';
>}
>print My::Base->new->some_method;
>__END__
>Can't locate object method "some_method" via package "My::Base" at
> inherit.pl line 16.
>
> Changing the some_method attribute to the following makes this go away:
>
>has some_method => ( is => 'rw', default => 'foo' );
>
> (is => 'ro', default => 'foo') also works.
>
> Is this a bug or have I misunderstood something?  This is Moose 0.79 and
> perl, v5.8.8 built for i486-linux-gnu-thread-multi. (Also fails on Solaris,
> so I doubt it's an OS issue).
>
> If this is a bug, I'll file a report.
>
> Cheers,
> Ovid
> --
> Buy the book - http://www.oreilly.com/catalog/perlhks/
> Tech blog- http://use.perl.org/~Ovid/journal/
> Twitter  - http://twitter.com/OvidPerl
> Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
>
>


-- 
Regards,
Mikhail


Re: Unfun with roles.

2009-04-23 Thread mikhail maluyk
Hope that clarify things

#!/usr/bin/perl -w

package Foo;

use Moose::Role;

sub foo {
print "I am foo\n";
};


package Bar;

use Moose::Role;

sub foo {
print "I am bar\n";
};


package Baz;

use Moose;

with
'Foo' => { excludes => [ 'foo' ] } ,
'Bar' => {};

# or
#with
#'Foo' => { excludes => 'foo' } ,
#'Bar' => {};

package main;

my $baz = Baz->new;
$baz->foo;


On Fri, Apr 24, 2009 at 9:13 AM, Elliot Shank  wrote:

> Call me confused about what's going on with multiple roles with methods
> having the same name.  I thought exclude should resolve this situation.
>  However, this complains about Baz having to implement foo(); what am I not
> understanding?
>
>
> package Foo;
>
> use Moose::Role;
>
> sub foo {};
>
>
> package Bar;
>
> use Moose::Role;
>
> sub foo {};
>
>
> package Baz;
>
> use Moose;
>
> with
>  Foo => { exclude => 'foo' },
>  Bar => {};
>
>


-- 
Regards,
Mikhail


Re: MooseX::Getopt and 'required'

2009-03-24 Thread mikhail maluyk
Hi,
Something like this should work

package App::Base;
use Moose;

has 'db_user' => ( isa => 'Str' , is => 'rw' );
has 'db_pass' => ( isa => 'Str' , is => 'rw' );

package App::FromCmd;
use Moose;

extends 'App::Base';
 with 'MooseX::Getopt';

has '+db_user' => ( required => 1 );
has '+db_pass' => ( required => 1 );

package App::Other;
use Moose;

extends 'App::Base';

package main;

my $other = App::Other->new; # okay
my $from_cmd = App::FromCmd->new; # fails, db_user and db_pass required

On Tue, Mar 24, 2009 at 5:07 PM, Zbigniew Lukasiak  wrote:

> I am writing a library with a command line utility (this if for
> generating FormHandler forms) - it takes as a parameter a
> DBIx::Class::Schema.  Obviously for the for the command line utility I
> cannot pass the schema object itself - but instead I pass the
> connection params and the schema class name and build that schema on
> the fly.  I would like to make those additional params 'required' for
> the command line utility - but not for the library API version.  This
> sounds like a common thing - what would be the common solution here?
> Can I just change the 'required' field option in a Subclass?
>
> --
> Zbigniew Lukasiak
> http://brudnopis.blogspot.com/
> http://perlalchemy.blogspot.com/
>



-- 
Regards,
Mikhail


Making "writer" changed in inheritance

2009-03-17 Thread mikhail maluyk
Hello everybody
package Foo;
use Moose::Role

has 'file' => ( isa => 'Str' , is => 'rw');

package Bar;
use Moose;

with 'Foo'

has '+file' => ( writer => 'set_foo_file' );

It wouldn't work, breaking with: "Illegal inherited options". I'm wondering
if this restriction could be overridden somehow?
Docs says that it could with using my own meta class, is it okay to add
"writer" to @legal_options_for_inheritance?

-- 
Regards,
Mikhail


Re: Introduction and DBIx::Class + Moose help

2008-12-15 Thread mikhail maluyk
Hello Doran,
Take a look at MooseX-DBIC
However there is a problem with MooseX::DBIC (or is it just me?), which i
was going to email to the list, but rather would tell about it in this
thread, hope you don't mind. When i'm using database in anyway (select,
insert), i'm getting the following:

Subroutine initialize redefined at /usr/lib/perl5/site_perl/5.10/Class/C3.pm
line 70.
Subroutine uninitialize redefined at
/usr/lib/perl5/site_perl/5.10/Class/C3.pm line 88.
Subroutine reinitialize redefined at
/usr/lib/perl5/site_perl/5.10/Class/C3.pm ine 101.

Both Moose and DBIx::Class are using Class::C3 and i think that's where the
problem arise. All functionality is working properly, but that warning is
annoying me.

2008/12/15 Doran L. Barton 

> Hello. I am a 11-year Perl programmer with lots of CGI, mod_perl, and some
> Catalyst web-development under my belt as well as a fair share of systems
> administration scripting as well.  I am happy to be a member of the Moose
> list.
>
> I've investigated Moose enough to have a feeling I really want to learn
> more
> about it. I have a web application I'm designing and would like to put
> Moose
> through the paces with it, but am having trouble figuring out how to best
> lay
> things out.
>
> The application will use DBIx::Class for database access. I'm having
> trouble
> figuring out how to mix Moose and DBIx::Class in my objects since they both
> magically create object methods. Does anyone have examples they could
> share?
> Something real simple, like the oft-used books+authors DBIx::Class examples
> but with Moose thrown in?
>
> Thanks!
>
> --
> Doran L. "Fozz" Barton 
> Open-source developer, sysadmin, consultant, and all-around geeky dude
>  "Take notice: When this sign is under water, this road is impassable."
>-- Seen on an Athi River highway
>
>


-- 
Regards,
Mikhail