scope in DBIx::Class::Core subclass

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

scope in DBIx::Class::Core subclass

Erik Colson
Hi

I have a Resultsource class as follows:

########################################

package myDB::Atable;

use base qw/ DBIx::Class /;

__PACKAGE__->load_components( qw/ PK::Auto Core / );
__PACKAGE__->table( 'atable' );

__PACKAGE__->add_columns( acolumn => {} );

__PACKAGE__->mk_group_ro_accessors( 'simple' => qw/ aCalculatedField / );

sub get_simple {
  my ( $self, $key ) = @_;
  if ( $key eq 'aCalculatedField' ) {
     return 'CALC: '.String::Util::trim( $self->acolumn );
  }
}

########################################

So now I can query Atable and get the columns 'acolumn' and
'aCalculatedfield'.

the column 'acolumn' will be returned by: $row->get_column('acolumn')
the calculated column will be returned by: $row->get_simple('aCalculatedfield')

However, the code doesn't work if I put a 'use String::Util;' at the top
of the package and replace 'String::Util::trim' by 'trim'. It looks like
the declaration gets out of scope.

How can I keep this code clean ?
--
erik colson

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: scope in DBIx::Class::Core subclass

Charlie Garrison
Good morning,

On 15/2/15 at 9:51 PM +0100, Erik Colson <[hidden email]> wrote:

>However, the code doesn't work if I put a 'use String::Util;' at the top
>of the package and replace 'String::Util::trim' by 'trim'. It looks like
>the declaration gets out of scope.

You need to either import the `trim` sub:

use String::Util 'trim';
......
return 'CALC: '.trim( $self->acolumn );


Or you need to call the sub with the package name as you were doing before:

return 'CALC: '.String::Util::trim( $self->acolumn );


Charlie

--
   Charlie Garrison  <[hidden email]>
   github.com/cngarrison   metacpan.org/author/CNG

O< ascii ribbon campaign - stop html mail
http://www.ietf.org/rfc/rfc1855.txt


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: scope in DBIx::Class::Core subclass

Erik Colson
Charlie Garrison <[hidden email]> writes:

> You need to either import the `trim` sub:
>
> use String::Util 'trim';

Awesome! thanks
--
erik colson

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Loading...