Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Mouse role fails

by karlgoethebier (Curate)
on Nov 02, 2012 at 12:52 UTC ( #1001962=perlquestion: print w/ replies, xml ) Need Help??
karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i'm playing around with Mouse roles and got stuck.

Role #1:

package ReadFile; use Mouse::Role; use MyTypes; use IO::File; requires qw(lines); has '_io' => ( is => 'ro', isa => 'MyCompany::ReadFile', builder => '_build_read_file', handles => [ qw( open getlines close) ], init_arg => undef, lazy => 1, ); sub _build_read_file() { my $self = shift; IO::File->new(); } # sub read_file() { # my ( $self, $file ) = @_; # $self->open( $file); # my @lines = $self->getlines(); # $self->close(); # chomp(@lines); # $self->lines(\@lines); # } 1;

This one fails on init with the followings errors:

Could not load class (ReadFile) because : Undefined subroutine &IO::Handle::_create_getline_subs called at C:/strawberry/perl/lib/IO/Handle.pm line 434.

In Handle.pm Line 434: # Special XS wrapper to make them inherit lexical hints from the caller. _create_getline_subs(<<'END' ) or die $@;

Compilation failed in require at C:/strawberry/perl/lib/IO/Seekable.pm line 101.

In Seekable.pm line 101: use IO::Handle ();

Compilation failed in require at C:/strawberry/perl/lib/IO/File.pm line 133.

In File.pm line 133: use IO::Seekable;

But the next one works.

Role #2:

package DBIxODBC; use Mouse::Role; use MyTypes; use DBIx::Simple; requires qw( user passwd dsn ); with qw(Timestamp); has 'data' => ( is => 'rw', isa => 'HashRef', trigger => \&_load, ); has '_dbix' => ( is => 'ro', isa => 'MyCompany::DBIx', handles => [qw( insert)], builder => '_build_dbix', init_arg => undef, lazy => 1, ); sub _build_dbix() { my $self = shift; my $connect_string; $connect_string = "dbi:ODBC:" . $self->dsn; DBIx::Simple->new( $connect_string, $self->user, $self->passwd ); } sub _load() { my ( $self, $input ) = @_; my $table = ( keys %$input )[0]; $self->insert( $table, $input->{$table} ); print $self->timestamp . qq(\n); } 1;

Thank you very much for any advice.

Best regards,

Karl

Comment on Mouse role fails
Select or Download Code
Re: Mouse role fails
by tobyink (Abbot) on Nov 02, 2012 at 12:59 UTC

    It looks like there may be a problem with your copy of IO::Handle. Does this script work OK for you?

    use IO::Handle; STDOUT->print("Type some text and hit enter.\n"); STDOUT->print("Type 'quit' or 'exit' when you've finished.\n\n"); while (my $x = STDIN->getline) { last if $x =~ /^\s*(quit|exit)/i; STDOUT->print($x); }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      It works as expected.

      Hi, this code fails with Active State Perl and Strawberry but is OK on a linux box:

      use IO::File; use Data::Dumper; my $fh = IO::File->new(); $fh->open(shift); my @lines = $fh->getlines(); undef $fh; print Dumper (@lines);

      Perl versions:

      Strawberry: This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x86-multi-thread

      Active State: This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x86-multi-thread

      Linux Box Perl: This is perl, v5.8.8 built for x86_64-linux-thread-multi

      It seems that the role didn't fail but IO::File/IO::Handle does(same errors), as you assumed.

      Regards and thank you, Karl

        If ActiveState and Strawberry are on the same box, it might be that they are getting confused about their libraries. XS libraries built for one are very unlikely to run correctly on the other.

        Looks like I somehow duplicated this post instead of updating it!

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        If ActiveState and Strawberry are on the same box, it might be that they are getting confused about their libraries. XS libraries built for one are very unlikely to run correctly on the other.

        Also, for what it's worth, Moo is gaining traction over Mouse as a light-weight Moose replacement. Although its surface syntax is less Moose-compatible, its killer feature is that if Moo detects that Moose has been loaded, it can inflate Moo classes and roles into full Moose classes and roles.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Am I missing what this is (not) supposed to do? Seems to work fine for me on Windows 7 x64 and Strawberry 5.16.1 (64-bit):

        VinsWorldcom@C:\Users\VinsWorldcom\tmp> ver Microsoft Windows [Version 6.1.7601] VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl -v This is perl 5, version 16, subversion 1 (v5.16.1) built for MSWin32-x +64-multi-thread VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl test.pl test.pl $VAR1 = 'use IO::File; '; $VAR2 = 'use Data::Dumper; '; $VAR3 = 'my $fh = IO::File->new(); '; $VAR4 = '$fh->open(shift); '; $VAR5 = 'my @lines = $fh->getlines(); '; $VAR6 = 'undef $fh; '; $VAR7 = 'print Dumper (@lines);';

      Hi all, here is what i did to fix this issue for Active State Perl:

      C:/perl/bin/perl.exe -MCPAN -e shell

      Then:

      cpan> upgrade File::IO

      Sorry i didn't capture the output - i was a little bit annoyed and impatient. But it worked.

      For Strawberry was no upgrade available.

      To figure out this issue (two Perl installations on same system), i will throw away all Perl on my box and restart with Active State Perl.

      Regards, Karl

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001962]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-09-19 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (132 votes), past polls