FAQ
Hi,

I am new in perl, so please forgive me if it is stupid.

I tried to use $c->comp("MyApp::M::CDBI::Book") to get a class for the
"Book" table, but turns out it returned me the class for
$c->comp("MyApp::M::CDBI::Booklet").

I traced down into Catalyst::Engine::component:
sub component {
my $c = shift;
if (@_) {
my $name = shift;
if ( my $component = $c->components->{$name} ) { #
------------------- (*)
return $component;
}
else {
# try to match $name as regex
}
}
}

I found that $component has obtained the correct class for
"MyApp::M::CDBI::Book" in (*) but the "if" condition FAILED and hence
control jumped to the else part, matching the "MyApp::M::CDBI::Booklet"
class instead.

I tried modifying it to
- if ( my $component = $c->components->{$name} ) {
+ if ( defined( my $component = $c->components->{$name} ) ) {
then it works.

Anyone can tell me what did I do wrong? Thanks a lot!

--
Regards,
Alan

Search Discussions

  • Bernhard Graf at Sep 24, 2005 at 2:53 pm

    On Saturday 24 September 2005 14:32, Alan Tam wrote:

    I tried to use $c->comp("MyApp::M::CDBI::Book") to get a class for
    the "Book" table, but turns out it returned me the class for
    $c->comp("MyApp::M::CDBI::Booklet").
    Welcome to my club. ;-)
    Anyone can tell me what did I do wrong? Thanks a lot!
    http://lists.rawmode.org/pipermail/catalyst/2005-August/001464.html
    --
    Bernhard Graf
  • Alan Tam at Sep 24, 2005 at 3:41 pm

    Bernhard Graf wrote:

    Anyone can tell me what did I do wrong? Thanks a lot!
    http://lists.rawmode.org/pipermail/catalyst/2005-August/001464.html
    Thanks for your pointer! No matter I agree with whether regex is good or
    bad, no reason can we cast the class to its boolean context.

    Given my limited perl knowledge, I can workaround by either patching
    Class::DBI to always return true or Catalyst::Engine to use defined().
    Any more choice for me?

    My "MyApp::M::CDBI" looks like below and I do not define all other
    MyApp::M classes.

    package MyApp::M::CDBI;
    use strict;
    use base "Catalyst::Model::CDBI::CRUD";
    __PACKAGE__->config(%{YAML::LoadFile("$ENV{CATALYST_HOME}conf/database.yml")});
    1;

    --
    Regards,
    Alan

    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20050924/ebc6c4b0/attachment.htm
  • Matt S Trout at Sep 24, 2005 at 4:25 pm

    On Sat, Sep 24, 2005 at 09:45:06PM +0800, Alan Tam wrote:
    Anyone can tell me what did I do wrong? Thanks a lot!
    http://lists.rawmode.org/pipermail/catalyst/2005-August/001464.html
    Thanks for your pointer! No matter I agree with whether regex is good or
    bad, no reason can we cast the class to its boolean context.

    Given my limited perl knowledge, I can workaround by either patching
    Class::DBI to always return true or Catalyst::Engine to use defined().
    Any more choice for me?
    my $comp = $c->comp('M::Book$');

    This is the approach I use and not only works for Class::DBI stuff but means
    your components get a head-start on portability between apps.

    --
    Matt S Trout Specialists in perl consulting, web development, and
    Technical Director UNIX/Linux systems architecture and automation. Mail
    Shadowcat Systems Ltd. mst (at) shadowcatsystems.co.uk for more information

    + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +
  • Alan Tam at Sep 24, 2005 at 5:04 pm

    Matt S Trout wrote:
    my $comp = $c->comp('M::Book$');

    This is the approach I use and not only works for Class::DBI stuff but means
    your components get a head-start on portability between apps.
    Unfortunately other classes, like Scaffold, would still call $c->compin
    their own way.

    But if it is my own code, why not just use a string to represent the class.

    --
    Regards,
    Alan
  • Matt S Trout at Sep 24, 2005 at 5:16 pm

    On Sat, Sep 24, 2005 at 11:08:33PM +0800, Alan Tam wrote:
    Matt S Trout wrote:
    my $comp = $c->comp('M::Book$');

    This is the approach I use and not only works for Class::DBI stuff but
    means
    your components get a head-start on portability between apps.
    Unfortunately other classes, like Scaffold, would still call $c->compin
    their own way.

    But if it is my own code, why not just use a string to represent the class.
    Re-usability, as I already mentioned. Plus the fact that $c->comp will
    return an instantiated object with the appropriate config if the class
    inherits from Catalyst::Base - and the fact that having one standard
    convention throughout my code makes life easier.

    --
    Matt S Trout Specialists in perl consulting, web development, and
    Technical Director UNIX/Linux systems architecture and automation. Mail
    Shadowcat Systems Ltd. mst (at) shadowcatsystems.co.uk for more information

    + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +
  • Alan Tam at Sep 24, 2005 at 6:20 pm

    Matt S Trout wrote:

    But if it is my own code, why not just use a string to represent the class.
    Re-usability, as I already mentioned. Plus the fact that $c->comp will return an instantiated object with the appropriate config if the class inherits from Catalyst::Base - and the fact that having one standard convention throughout my code makes life easier.
    I heard about the config feature, which allow me to initialize config
    for a class w/o specifying any code in that class. But I never get it
    working. Currently I need to file
    __PACKAGE__->config(YAML::LoadFile(...)); for each class, which I think
    is suboptimal.

    I also noticed Catalyst::Plugin::Config::YAML whose documentation seems
    to imply that I can put all config into one YAML file and use the
    section of the file to indicate which class the keys are for.

    Is there any pointer to working code?

    --
    Regards,
    Alan
  • Chisel Wright at Sep 25, 2005 at 8:11 am

    On Sun, Sep 25, 2005 at 12:24:57AM +0800, Alan Tam wrote:
    I also noticed Catalyst::Plugin::Config::YAML whose documentation seems
    to imply that I can put all config into one YAML file and use the
    section of the file to indicate which class the keys are for.

    I've not used the plugin, but this is in the cookbook:

    perldoc Catalyst::Manual::Cookbook
    - or -
    http://search.cpan.org/~mramberg/Catalyst/lib/Catalyst/Manual/Cookbook.pod#Configure_your_application


    --
    Chisel Wright
    e: chisel@herlpacker.co.uk
    w: http://www.herlpacker.co.uk/
  • Alan Tam at Sep 25, 2005 at 8:25 am

    Chisel Wright wrote:
    On Sun, Sep 25, 2005 at 12:24:57AM +0800, Alan Tam wrote:

    I also noticed Catalyst::Plugin::Config::YAML whose documentation seems
    to imply that I can put all config into one YAML file and use the
    section of the file to indicate which class the keys are for.
    I've not used the plugin, but this is in the cookbook:

    perldoc Catalyst::Manual::Cookbook
    - or -
    http://search.cpan.org/~mramberg/Catalyst/lib/Catalyst/Manual/Cookbook.pod#Configure_your_application
    I think I've messed things up. I've read the Cookbook many times, and it
    really seems too short for the need of troubleshooting. For times I have
    to look into the source code of various packages to learn what happened.

    I suppose I was solely referring to Catalyst::Plugin::Config::YAML for
    that feature, which seems to allow me to specify the config file so it
    can call YAML::LoadFile for me, and for every class. If no one really
    uses it, then I will experiment with it myself.

    --
    Regards,
    Alan

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedSep 24, '05 at 2:28p
activeSep 25, '05 at 8:25a
posts9
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2021 Grokbase