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

Fello Monks

Given this code snippet:

sub home { my $self = shift; my $html_template = $self->param('html_template'); my $cgi = CGI->new; my $session = $cgi->param('session'); my $empty; my $user_status; my ($session_id, $last_action_date, $last_action_time) = $session =~ + /^(\d\d)-(\d{10})-(\d{8})$/; my $datetime = Class::Date->now(); $datetime =~ tr/://d; my ($now_date, $now_hour, $now_min, $now_seconds) = $datetime =~ /^ +(\d{8}) (\d{2})(\d{2})(\d{2})$/; my $output; $html_template->process('home', { wrapper => $self->wrapper(), session => $session, user_status => $user_status, session_id => $session_id, now_date => $now_date, now_hour => $now_hour, now_min => $now_min, now_seconds => $now_seconds, session_id => $session_id, last_action_date => $last_action_date, last_action_time => $last_action_time, }, \$output) || die $html_template->error; return $output; }

Why wont all the now_ variables display on the relevant template when they are called in the template correctly?

In my Base.pm, I do something similar, but the varibles (now_ in the snippet) are put into an array, and used from there as an array

So my understanding is that the variables in the my (), are an array, so how can the array be split up, or am I approaching this wrong?

Update: And same question regarding the $session "split" with session_id, last_action_whatever, where $session is a string of numbers (say 00-0000000000-00000000)

Update: So wht i want to do is take $session, and put the first 2 digits/chars. into $session_id, then next 10 into $last_action_date, then the next 8 into $last_action_time, and use them on the page/in the rest of the function, and I dont care if my $session layout os wrong, Coz the question will still stand!

Yours

FINAL UPDATE: All Sorted thanks to the Split Function suggested by ptum, its all I wanted to do was Split up a $memory and put the split bits into $other_mems

Barry Carlyon barry@barrycarlyon.co.uk

Replies are listed 'Best First'.
Re: Large Variable to small variables, why dont they display
by Asim (Hermit) on Jun 20, 2006 at 17:29 UTC

    Barry,

    A bit of "teach a man to fish", if you will. I did this reply on debugging Template Toolkit a few days ago. When I have a weird missing-data-in-Template situation like yours, whipping out [% USE Dumper %], and printing out the response, has been the starting point on resolving many a problem.

    Does that make sense, and help?

    ----Asim, known to some as Woodrow.

Re: Large Variable to small variables, why dont they display
by ptum (Priest) on Jun 20, 2006 at 16:49 UTC

    In the updated example, the string '00000000000000000000' would not match the regex you specify for session (which expects hyphens), and therefore the contents of $session_id, $last_action_date, and $last_action_time would all be undefined. I usually do something like this (admittedly more verbose code):

    my ($session_id, $last_action_date, $last_action_time) = (0,0,0); if ($session =~ /^(\d\d)-(\d{10})-(\d{8})$/) { $session_id = $1; $last_action_date = $2; $last_action_time = $3; } else { # print some kind of warning -- the session ID didn't match my regex +! }

    I have generally found that reliance on a regex match for code flow can be problematical ... external data is not always what I think it will be. You need to protect yourself from disaster a little more effectively.

    Update: I see that you updated your session example ... even so, the rest of the post has some merit, I think. I would probably use split /-/ for that particular case, anyway, and test that the split produced the requisite number of tokens before I rushed off to use the data.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Large Variable to small variables, why dont they display
by Fletch (Bishop) on Jun 20, 2006 at 16:34 UTC

      Well in that case, what about same question for the $session 'split'

      Barry Carlyon barry@barrycarlyon.co.uk
Re: Large Variable to small variables, why dont they display
by ikegami (Patriarch) on Jun 20, 2006 at 16:44 UTC
    What's $html_template? A package name? What's in that package?

      its template toolkit, it just declares a header file

      Barry Carlyon barry@barrycarlyon.co.uk

        Variable don't "decalare a header file", whatever that means.

        For $html_template->process to work, $html_template needs to reference either a package name or an object. Guessing from param, I bet it's the former. What's in that package? We can't debug code we don't see! (Unless the problem is that $session doesn't really contain something like 00-0000000000-00000000 as you say.)