当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - Call to Subroutine from Catalyst Controller

问题描述:

I am working on catalyst to design a front end for a database. I am new to perl and catalyst. I am stuck with one error in the controller.

I want to fetch gene names from one table and pass each gene name to a subroutine gene_name(call a subroutine gene_name) which will perform certain function. I some how fetched the column with gene names but they are in the form of Hash reference to other table. My call to gene_name is not working.

Any idea how to pass the values to the subroutine rather than reference?

My code is as follows:

my @gene_list = $c->model('Gene::GeneTable')->search({

},{

column =>[qw/symbol/],

}

);

foreach my $gene (@gene_list){

push @gene_aliases, &gene_name($gene);

}

The error I am getting after executing the code is as follows:

DBIx::Class::ResultSet::find(): Can't bind a reference (MyApp::Model::Gene::GeneTable=HASH(0x7ff6d88b7c58))

UPDATED

My gene_name() subroutine is in another module which I have included in the controller. Now I have changed the for loop(in controller) as following and currently it is passing gene name to gene_name() to fetch aliases(But query remains the same):

foreach my $gene (@gene_list){

push @gene_aliases, &gene_name($gene-> symbol);

}

I am accessing the @gene_aliases in my view file as follows:

[% FOREACH g in gene_aliases -%]

[% g %]

[% END -%]

The above code is fetching hash references instead of names in the page. I am not able to display the gene names on web page.If I give [% g.symbol %] in for loop then the page would be empty with no error message.Hope this would be sufficient information for answering my question. If not I would elaborate on it.

My SQL query is as follows:

Select Symbol from GeneTable;

Thanks in advance

UPDATED2

As I am not a full time programmer, I am asking these questions on the blog. Kindly help me resolve my issues. If in case I have search form which takes gene name from user and this gene name has to be passed to the gene_name() subroutine, how do I pass this (to the controller and how to call the gene_name()). Kindly help me in resolving this issue.

My form in view.tt is as follows:

<form method="post" action "[% c.uri_for('/gene')%]">

<input type="text" name="search_alias">

<input type="submit" name="alias_search" value="Search">

</form>

All my aliases are in the other table called Alias, which is been used in gene_name() subroutine.

网友答案:

Either the gene_name() function needs to expect a GeneTable object being passed to it, or you would call it like this: gene_name($gene->symbol).

Having said that, the Controller does not look like the right place for this, based on what limited information you've provided.

It would make a lot more sense to have gene_name as a method of Gene::GeneTable itself, ie:

package GeneTable;

...

sub gene_name {
    my $self = shift;
    my $gene_name = ... # do something with $self->symbol
    $gene_name
}

...

So that the ->gene_name method is available to any GeneTable object in any context.

UPDATE following OP's initial two comments

It sounds like gene_name() (which possibly should be called gene_aliases()) is a method of a Gene object. Putting such a conversion into a Controller does not adhere to good MVC philosophy.

Presumably you have model code somewhere - MyApp/Model/Gene/ most likely - and this sub should exist inside Gene/GeneTable.pm. Then you could use it thus:

[%- FOREACH g IN gene_list -%]
    [% g.symbol %]<br/><ul>
    [%- FOREACH gn IN g.gene_name -%]
    <li>[% gn %]</li>
    [%- END -%]
    </ul>
[%- END -%]

in your template, and anywhere else you have a GeneTable object or collection thereof.

UPDATE #2 Following updated question

DBIx:Class will return an array of objects when called in list context as you do. Perhaps you need to add some Data::Dumper->Dumper() calls to get a handle on what each step is returning to you.

I suggest you add the following line immediately after your call to ->search():

$c->log->debug("gene_list contains: ", Data::Dumper->Dumper(\@gene_list));

...and possibly the equivalent debug for @gene_aliases after you've populated that.

That might give you a clearer picture of what you're getting back from your search, but I'd hazard a guess you're getting Gene::GeneTable objects.

分享给朋友:
您可能感兴趣的文章:
随机阅读: