http://qs321.pair.com?node_id=682739

k0rn has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem with a cookie routine

if ($register eq 1) {
my $cookie = cookie(

-NAME => 'zebulun.ath.cx',
-VALUE => '$key',
-EXPIRES => '+12h',
);
print header(-cookie => $cookie);
}

The problem is the -VALUE => '$key'; instead of useing the data in the variable $key it uses it as text. Is their a way to fix that??

Replies are listed 'Best First'.
Re: Cookie Help
by hipowls (Curate) on Apr 24, 2008 at 23:56 UTC

    That is because you have $key between single quotes. Remove them like so

    my $cookie = cookie( -NAME => 'zebulun.ath.cx', -VALUE => $key, -EXPIRES => '+12h', );

      Oh duh. Wow i feel stupid. Attention to detail. One more question i keep getting this trailing 1 on the page when it displays information. And i don't know why. For example like this.

      1

      Identified

      You are identified as

        Look at your print statements. There probably is one that shouldn't be there.

        Also, your condition should look like the following. The equality operator == is to be used for numerical comparisons.

        if ($register == 1) {
Re: Cookie Help
by jethro (Monsignor) on Apr 25, 2008 at 03:11 UTC
    Well, if the code you posted above is practically all there is, then probably header doesn't return a string, but 1 (true) on success and 0 (false) otherwise. Check the module documentation

    If that's not the case you would have to post some more code to get an answer to your second question. Or (even better) you could try to find the exact location yourself. Put in some print statements in your code to see where the '1 originates. For example:

    print header(-cookie=> $cookie); print "XX";
    If you see the '1' before 'XX' in your browser, then your call to header produced the '1', otherwise the '1' is generated afterwards.

    You might surround all print statements or calls to library routines you suspect of producing the '1' with these print statements (each printing a different string) until you have narrowed down the location of the bug.

    On another tangent: Is the string compare in if ($register eq 1) there on purpose ?
    To compare numbers one should use == generally.