Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found