[Catalyst] Re: difficulty in assigning an array to a stash

jagdish eashwar jagdish.eashwar at gmail.com
Mon Nov 12 15:18:22 GMT 2007


Hi,

Thank you for all the responses. I must admit that I never realised
that what I was getting was not the last value but the number of
elements in the array. Anyway, in the course of my experimentation, I
did make some half hearted attempts with '$c->stash->{myoptions} =
\@options;' yesterday, but instead of the options, I was getting a
list of 'myleave::Model::myleavedb::txn_mdl=HASH(some_number) in the
tt2 template. When I tried again today after your replies, I got the
same result. Here is my code:

sub cancel : Local {
	my ($self,$c) = @_;
	my @options = $c->model('myleavedb::txn_mdl')->search({emp_no =>
$c->user->emp_no},{columns => [qw/lv_id/]});
	$c->stash->{myoptions} = \@options;
	$c->stash->{template} = 'leavedata/cancel2_frm.tt2';
	}

Then I added a foreach loop as under and I got the expected results in
the tt2 template. I also had to declare @new_options at the top.

sub cancel : Local {
	my ($self,$c, at new_options) = @_;
	my @options = $c->model('myleavedb::txn_mdl')->search({emp_no =>
$c->user->emp_no},{columns => [qw/lv_id/]});
	foreach my $option(@options) {
	push (@new_options,$option->lv_id)
	}
	$c->stash->{myoptions} = \@new_options;
	$c->stash->{template} = 'leavedata/cancel2_frm.tt2';
	}

I don't understand in what way @options and @new_options are different
from each other, but in a bumbling manner I have made the code work.
What is the preferred way of writing this bit of code?

jagdish eashwar
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 11 Nov 2007 21:58:04 +0530
> From: "jagdish eashwar" <jagdish.eashwar at gmail.com>
> Subject: [Catalyst] difficulty in assigning an array to a stash
> To: catalyst at lists.scsys.co.uk
> Message-ID:
>         <f1e12d440711110828j671eae6amf5142912b587ea9 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi,
> I am trying to retrieve into an array a list of values from a column
> in a table using dbic and have it displayed in a drop down list in a
> tt2 template. I am able to do the retrieval part, but the tt2 template
> is displaying only the last value from the array. With further
> experimentation, I found that if I explicitly assign an array to a
> stash ( like '$c->stash->{myoptions} = [qw/1 2 3 4 5 6 7/];' ), I am
> getting all the 7 values in the tt2 template drop down. But if I
> define the array first and then assign it to the stash, ( like 'my
> @options = qw/1 2 3 4 5 6 7/; $c->stash->{myoptions} = @options;) I am
> getting only the last value in the tt2 template. How can I get all the
> values in the array into the stash without having to do it explicitly?
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 11 Nov 2007 11:37:47 -0500
> From: Andy Grundman <andy at hybridized.org>
> Subject: Re: [Catalyst] difficulty in assigning an array to a stash
> To: The elegant MVC web framework <catalyst at lists.scsys.co.uk>
> Message-ID: <245A9749-BDB5-4514-A68C-AEC53D415292 at hybridized.org>
> Content-Type: text/plain; charset=US-ASCII; format=flowed
>
>
> On Nov 11, 2007, at 11:28 AM, jagdish eashwar wrote:
>
> > Hi,
> > I am trying to retrieve into an array a list of values from a column
> > in a table using dbic and have it displayed in a drop down list in a
> > tt2 template. I am able to do the retrieval part, but the tt2 template
> > is displaying only the last value from the array. With further
> > experimentation, I found that if I explicitly assign an array to a
> > stash ( like '$c->stash->{myoptions} = [qw/1 2 3 4 5 6 7/];' ), I am
> > getting all the 7 values in the tt2 template drop down. But if I
> > define the array first and then assign it to the stash, ( like 'my
> > @options = qw/1 2 3 4 5 6 7/; $c->stash->{myoptions} = @options;) I am
> > getting only the last value in the tt2 template. How can I get all the
> > values in the array into the stash without having to do it explicitly?
>
> $c->stash->{myoptions} = \@options;
>
>
>
> ------------------------------
>
> Message: 3
> Date: Sun, 11 Nov 2007 16:40:31 +0000
> From: "Mark Smith" <smitty at gmail.com>
> Subject: Re: [Catalyst] difficulty in assigning an array to a stash
> To: "The elegant MVC web framework" <catalyst at lists.scsys.co.uk>
> Message-ID:
>         <e417a7bb0711110840xa746ad8g3e67085a2995e8c5 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> > But if I
> > define the array first and then assign it to the stash, ( like 'my
> > @options = qw/1 2 3 4 5 6 7/; $c->stash->{myoptions} = @options;) I am
> > getting only the last value in the tt2 template. How can I get all the
> > values in the array into the stash without having to do it explicitly?
>
> That's because you're stuffing an array in a scalar.  Or trying.  Do this:
>
> my @options = qw/1 2 3 4 5 6 7/;
> $c->stash->{myoptions} = \@options;
>
> Or, for a literal translation of your first code:
>
> my $options = [ qw/1 2 3 4 5 6 7/ ];
> $c->stash->{myoptions} = $options;
>
> Note the differences.
>
> While you're at it, try "perldoc perlreftut" and "perldoc perlref" for
> more information on references.
>
> Good luck.
>
>
> --
> Mark Smith / xb95
> smitty at gmail.com
>
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 11 Nov 2007 17:41:54 +0100
> From: Lars Balker Rasmussen <lars at balker.dk>
> Subject: Re: [Catalyst] difficulty in assigning an array to a stash
> To: The elegant MVC web framework <catalyst at lists.scsys.co.uk>
> Message-ID: <20071111164154.GF62319 at tux.nerdheaven.dk>
> Content-Type: text/plain; charset=us-ascii
>
> On Sun, Nov 11, 2007 at 09:58:04PM +0530, jagdish eashwar wrote:
> > Hi,
> > I am trying to retrieve into an array a list of values from a column
> > in a table using dbic and have it displayed in a drop down list in a
> > tt2 template. I am able to do the retrieval part, but the tt2 template
> > is displaying only the last value from the array. With further
> > experimentation, I found that if I explicitly assign an array to a
> > stash ( like '$c->stash->{myoptions} = [qw/1 2 3 4 5 6 7/];' ), I am
> > getting all the 7 values in the tt2 template drop down. But if I
> > define the array first and then assign it to the stash, ( like 'my
> > @options = qw/1 2 3 4 5 6 7/; $c->stash->{myoptions} = @options;) I am
> > getting only the last value in the tt2 template. How can I get all the
> > values in the array into the stash without having to do it explicitly?
>
>  $c->stash->{myoptions} = \@options;
>
>  perldoc perlreftut
> --
> Lars Balker Rasmussen                                        Consult::Perl
>
>
>
> ------------------------------
>
> Message: 5
> Date: Sun, 11 Nov 2007 20:52:04 +0100
> From: "A. Pagaltzis" <pagaltzis at gmx.de>
> Subject: [Catalyst] Re: difficulty in assigning an array to a stash
> To: catalyst at lists.scsys.co.uk
> Message-ID: <20071111195204.GD20669 at klangraum>
> Content-Type: text/plain; charset=us-ascii
>
> * Mark Smith <smitty at gmail.com> [2007-11-11 19:15]:
> > While you're at it, try "perldoc perlreftut" and "perldoc
> > perlref" for more information on references.
>
> Also `perldoc perllol` and `perldoc perldsc`.
>
> Regards,
> --
> Aristotle Pagaltzis // <http://plasmasturm.org/>
>
>
>
> ------------------------------
>
> Message: 6
> Date: Mon, 12 Nov 2007 10:12:08 +0100
> From: Felix Antonius Wilhelm Ostmann <ostmann at websuche.de>
> Subject: Re: [Catalyst] difficulty in assigning an array to a stash
> To: The elegant MVC web framework <catalyst at lists.scsys.co.uk>
> Message-ID: <473818E8.3040407 at websuche.de>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> jagdish eashwar schrieb:
>
> > I am
> > getting only the last value in the tt2 template.
>
> no, you count the array and save it into the scalar. your problem: count
> and last element are the same ;)
>
> use [@array] or \@array to save the reference.
>
>
>
> > How can I get all the
> > values in the array into the stash without having to do it explicitly?
> >
> > _______________________________________________
> > List: Catalyst at lists.scsys.co.uk
> > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> > Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> > Dev site: http://dev.catalyst.perl.org/
> >
> >
> >
>
>
> --
> Mit freundlichen Grüßen
>
> Felix Antonius Wilhelm Ostmann
> --------------------------------------------------
> Websuche   Search   Technology   GmbH   &   Co. KG
> Martinistraße 3  -  D-49080  Osnabrück  -  Germany
> Tel.:   +49 541 40666-0 - Fax:    +49 541 40666-22
> Email: info at websuche.de - Website: www.websuche.de
> --------------------------------------------------
> AG Osnabrück - HRA 200252 - Ust-Ident: DE814737310
> Komplementärin:     Websuche   Search   Technology
> Verwaltungs GmbH   -  AG Osnabrück  -   HRB 200359
> Geschäftsführer:  Diplom Kaufmann Martin Steinkamp
> --------------------------------------------------
>
>
>
>
> ------------------------------
>

>
> Message: 8
> Date: Mon, 12 Nov 2007 11:02:07 +0000
> From: Carl Johnstone <catalyst at fadetoblack.me.uk>
> Subject: Re: [Catalyst] difficulty in assigning an array to a stash
> To: The elegant MVC web framework <catalyst at lists.scsys.co.uk>
> Message-ID: <473832AF.2060400 at fadetoblack.me.uk>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> jagdish eashwar wrote:
> > I am getting only the last value in the tt2 template.
> You're actually getting the number of items in the list, because that's
> what you get when you turn a list into a single value.
>
> Carl
>
>
>
>
> ------------------------------
>
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>
>
> End of Catalyst Digest, Vol 33, Issue 23
> ****************************************
>



More information about the Catalyst mailing list