Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

$c->flash Not Dereferencing Data Objects

by phildeman (Scribe)
on Jul 02, 2020 at 19:48 UTC ( [id://11118836]=perlquestion: print w/replies, xml ) Need Help??

phildeman has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I encountered something unusual with Perl 5.14.2.

I am using DBIx::Class to query my database. The data object is returned to Root to be passed to a View.
I pass the data object to the View via $c->flash. However, in the View it has a problem dereferencing the
data object.

For example, I query a person's name and address and store it in bio_object. Then I pass it to $c->flash like so:

In Root.pm:

my $bio_obj = $model->get_bio_info( $id ); $c->flash->{ 'bio' } = $bio_obj; $c->res-redirect( 'myviewpage' );

In myviewpage, using Mason in HTML page:

<p>My name is <% $bio->name %><br /> My residence is at <% $bio->address %> <%init> my $bio = $c->flash->{ 'bio' } if $c->flash->{ 'bio' }; </%init>

The view error message states it cannot find the method 'name' or 'address'. However, I created a test script
that calls the same query method, "get_bio_info". I display the results without issues, using a print statement.

Has anyone experienced this before?

Replies are listed 'Best First'.
Re: $c->flash Not Dereferencing Data Objects
by Your Mother (Archbishop) on Jul 02, 2020 at 20:35 UTC

    I don’t have time to write test code but I think see the problem. You can only use a flash entry once. It’s gone—removed from the session—after that. The entire point is it cannot persist past a single use. Put it in the stash instead—unless you need it to persist past a redirect or something—and you should be fine. And just to be clear, this is actually a Catalyst question, not a DBIx::Class question. The flash/stash is part of the Catalyst context and its session.

    More detail, when you do if $c->flash->{bio}, you just used it so it’s gone by the time you get to the assignment. Also note, you should never put a conditional on an (update: variable declarative) assignment statement. It can behave strangely.

    # Unpredictable- my $x = 1 if $y; # Safe- my $x; $x = 1 if $y;

    Update in case you do need it as flash, untested–

    <%init> my $bio = $c->flash->{'bio'}; </%init> % if ( $bio ) { <p>My name is <% $bio->name |h %><br /> My residence is at <% $bio->address |h %></p> % }

    Update, added HTML escape filters. Been ages since I did Mason. :P NEVER trust user input.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-25 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found