Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Accessing Variables in Different Subroutines

by sauoq (Abbot)
on Mar 19, 2015 at 18:42 UTC ( [id://1120638]=note: print w/replies, xml ) Need Help??


in reply to Accessing Variables in Different Subroutines

1 - If I instantiate a variable outwith the subroutines and give it a value in one subroutine, I am unable to get that value in subsequent subroutines.

You are only calling one subroutine per execution. This should be obvious to you because you call it with $execute->(); and immediately thereafter, you exit;.

The mistake you are making is pretty common for people who are brand new to web development. You are thinking that multiple calls to your CGI script are handled within one execution of the script. That's not the case. The script is executing anew each time you submit your form.

There are different ways around this, but the most common one is to use a cookie to maintain state. Although you can store the state information directly in the cookie, that's not a very robust way to do it. A better way is to store the data on the server somewhere (often in a database) and then use the cookie as a key to get to that data.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: Accessing Variables in Different Subroutines
by stefl (Acolyte) on Mar 20, 2015 at 08:38 UTC

    Thanks very much. As you can guess, I only started looking at web development just over a week ago, so I'm very much a beginner. Your answer explained my mistake very well, thanks!
    I managed to get around my problem by using hidden parameters (I'd tried this previously but incorrectly and just assumed that it didn't work) and now my script does what I need it to.
    If I start needing to do more web development than I am just now, I'll have a deeper look into other options (but for now this works!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1120638]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found