You should check out Perl Dancer. It is a micro framework that has session support built in, which allows you to preserve values across otherwise unconnected HTTP requests. I took a little liberty with your code and "ported" it over to work in a Dancer framework. Using templates is a better choice but CGI.pm still works -- you just want to be sure and not use the header() function, as Dancer will send that data for you. I will confess that i could not get this code to work with POST requests, only GET requests. That can be an exercise for the viewer. :) The issue has something to do with CGI.pm defaulting to "multipart/form-data" enctypes ... and since you really should use templates instead of CGI.pm the right thing to do is modify this code to use templates.

Also, don't forget to edit your config.yml file in your Dancer project to turn session handling on. Have fun!

package My::App; use Dancer ':syntax'; use CGI qw(:all); our $VERSION = '0.1'; # Initial variables my @chars = ("A".."Z", "a".."z", "0".."9"); my $randomString; $randomString .= $chars[rand @chars] for 1..8; my @languages = ("English", "Francais"); my @colours = ("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black"); get '/' => sub { session random_str => $randomString; my $random_str = session 'random_str'; my $file = "$random_str.txt"; return start_html() . h2("Welcome to this basic form") . start_form( -action => 'select_color', -method => 'GET' ) . popup_menu({ -name => "language", -values => \@languages, -default => $languages[0] }) . p("The randomly generated string is: $random_str<br>The filena +me is: $file") . submit({-value => "Continue"}) . end_form() . end_html() ; }; get '/select_color' => sub { my $random_str = session 'random_str'; my $file = "$random_str.txt"; return start_html() . h2("Part Two") . start_form( -action => 'select_animal', -method => 'GET' ) . p("The randomly generated string is: $random_str<br>The filena +me is: $file") . hidden({ -name => "language", -value => params->{language} }) +. "<p>Please choose your favourite colour: </p>" . radio_group({ -name => "colour", -values => \@colours, -default => $colours[0], -linebreak => "true" }) . submit({-value => "Continue"}) . end_form() . end_html() ; }; get '/select_animal' => sub { my $random_str = session 'random_str'; my $file = "$random_str.txt"; return start_html() . h2("Almost Done!") . start_form() . p("The randomly generated string is: $random_str<br>The filena +me is: $file") . hidden({-name => "language", -value => params->{language}}) . hidden({-name => "colour", -value => params->{colour}}) . p("Thanks for your help!") . end_form() . end_html() ; }; true;

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Accessing Variables in Different Subroutines by jeffa
in thread Accessing Variables in Different Subroutines by stefl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.