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

Hey guys, So I have run into a weird issue and was wondering if anyone could help me... I have a segment of code which is designed to test data using PERL in a MASON file. The concept is that when the user enters a item for deletion, the PERL code identifies it and alerts the user to the number of items to delete... Here is a breakdown of the code:
% # my $tab_id is set higher in the code^^^ % my $foo = $c->req->param('foo'); % my $bar = $c->req->param('bar'); % my $baz = $c->req->param('baz'); % my @update = split(/,/, $c->req->param('biz'); #this is an array o +f place to delete # the items % if ( ($foo) && ($baz == 2) && (and{$tab_id == $_}@update) ) { % my @delete_items = split (/,/, $foo); % my $del_count = scalar(@delete_items); deletes += <%$del_count%>; # this is setting the JS variable f +or the counts % } % if ( ($bar) && ($baz == 2) && (and{$tab_id == $_}@update) ) { % my @delete_items = split (/,/, $bar); % my $del_count = scalar(@delete_items); deletes += <%$del_count%>; % } --------------------------------------------------------------------- +------------ // javascript section: if(deletes > 0) { return confirm('Delete ' . + deletes + . 'Items? '); }
for a bit of context here $foo, $bar are the input from the form and this section of code is executed along with other stuff on the user hitting submit on the form. My issue is that the code works, but it doesn't alert properly. its a bit hard to explain so I will just give an example.. Say I enter 3 items to be deleted and press submit. No error will pop up to alert me. But say I go back to the page and try to delete a single item, the alert will pop up and say 'Delete 3 Items? ' When I dump the variable contents to the log, the variables contain data from the first submission and not the second (correct) submission. This continues... Every time I do an operation on the page, the data that the alert operates on is one 'session' behind... The alert works, and it is getting the expected data but just needs to operate on the correct update... I am unsure if this is an issue with the way Catalyst is handling the data or what, but I could sure use some help... Thanks!!!

Replies are listed 'Best First'.
Re: Help with Perl/Catalyst/Mason
by Anonymous Monk on Jun 30, 2012 at 10:33 UTC

    First of all, please check the generated HTML (javascript) code, and see if there is anything abnormal about it. That is always your best bet to debug these sorts of things.

    Second, you ought to learn how to use Mason as a templating language. The Mason book is available for free online reading. Please read about the different blocks available (<%args>, <%init> at the very least), and for $DEITY's sake, please do not embed that much code in the middle of your HTML. Mason is fairly good in separating code and HTML. Here's your code rewritten:

    <%args> $foo $bar $baz $biz </%args> ... your html code ... deletes += <% $del_count %>; if(deletes > 0) { return confirm('Delete ' . + deletes + . 'Items? '); } ... your html code ... <%init> my @update = split(/,/, $biz); my $del_count = 0; if ($baz == 2 and and{ $tab_id == $_ } @update) { # ??? foreach ($foo, $bar) { $del_count += scalar split(/,/, $_); } } </%init>

    I don't know what is wrong with your code, but I do wonder what and{ $tab_id == $_ } @update is supposed to mean...

      Oh, are you expecting that Perl code to run when you press Submit? I mean, asking for confirmation before the form is submitted?

        Yes, I was hoping that the perl would run when you pressed submit, beofre the form posts.
      Thanks for the response... and{ $tab_id == $_ } @update = any{$tab_id == $_}@update. sorry for the typo. I am using this : List::MoreUtils qw( any ); Basically what it is doing is checking to see if the $tab_id value is located anywhere in the @update array. This allows me to control which tab the items are deleted from. Using the <%init> blocks like you have will the perl run prior to the deletes alert popping up? Thanks Again!!!