PDA

View Full Version : sessions - submitting/retrieving additional values


bazz
08-26-2007, 04:30 PM
Hello,

OK so I have a login file, which creates a session and then, which loads other files into an iframe.

I want to add more values to the session and I am getting an error on the retrieve part.


my $group = $query->param('inputListedGroup');

print qq(group=$group);#successful
$session->param('groupName', "$group");
print $session->groupName();



the error is:

Can't locate object method "groupName" via package "CGI::Session" at add_stage1.pl line 156.


Am I not entering the data to the session or am I trying to retrieve it wrongly.

bazz

bazz
08-26-2007, 04:42 PM
Got it sorted I think.

I looked at the CGI::Session in cpan and found this liitle gem :)

I have modified it to a more relative var name.


my $sessionstoredgroup = $session->param('groupName');



bazz

KevinADC
08-26-2007, 09:59 PM
Do you understand the difference between the two? Here you are calling a function (or method as it's called in OO terms) of the $session object:

print $session->groupName();


obviously that is not correct unless the CGI::Session module has a function of that name.

I see you are doing something very bad in your perl code, quoting a single scalar:

$session->param('groupName', "$group");

much better written as:

$session->param('groupName', $group);

the quotes around the scalar serve no purpose other than to slow down the script and can lead to very hard to find bugs because 'warnings' and 'strict' will not even help. What if you accidently typed this:

$session->param('groupName', "group");

perfectly valid syntax that will run without warning or error and not do what you expect. Trying to find a syntax mistake like that in a large program can be near impossible.

$foo = 'foo'; <-- good
$foo = "foo"; <-- OK, but better with single-quotes
$bar = 'bar'; <-- good
$foo = $bar; <-- good
$foo = "$bar"; <-- works but is a very bad habit to get into
$baz = 'baz'; <-- good
$foo = "$bar$baz"; <-- good because you made a new string from two scalars
if ("$foo" eq "$bar") { <-- works, but again, is a very bad habit to get into

bazz
08-27-2007, 01:42 AM
Kevin, thank you.

I wasn't aware of most of that. I have chnaged my code and wil read your post in more depth tomorrow.

Much appreciated

bazz

KevinADC
08-27-2007, 02:32 AM
The philosophy is pretty simple really: never quote scalars unless you need to make a string or use them in a string.

bazz
09-05-2007, 08:10 PM
OK I still have a problem.

Submitting a form and then arriving at this sub, the value stoired in the session seems not to be available



my $GroupName;
my $group = $query->param('inputListedGroup');
$session->param('groupName', $group);
$GroupName=$query->param('groupName'); #should be $session



The value is carried in the param 'inputListedGroup'
I store it to the session with '$session->param('groupName', $group);'

So why can't I retrieve it?

bazz

bazz
05-29-2008, 11:10 PM
OK, this is an old thread but my question now, is relevant to it.

is this a poor bit of code. I am thinking from the perpective of quoting scalars.


$session->param( "average.$room_id", $average_price );


I need to be able to store different pieces of data where it is relative to a variable - $room_id.

bazz