Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Accessing Variables in Different Subroutines

by jeffa (Bishop)
on Mar 19, 2015 at 21:22 UTC ( #1120665=note: print w/replies, xml ) Need Help??


in reply to Accessing Variables in Different Subroutines

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)

Replies are listed 'Best First'.
Re^2: Accessing Variables in Different Subroutines
by Anonymous Monk on Mar 20, 2015 at 08:04 UTC

    ...I will confess that i could not get this code to work with POST requests, only GET requests....get '/' => sub { ... get '/select_color' => sub { ... get '/select_animal' => sub { ...

    get is get, post is post, any is any of http methods get/put/post/delete

    just get/post is  any ['get', 'post'] => sub { ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1120665]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-05-26 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?