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

.. sections of my code
print header( -cookie => cookie(-name => 'Matrix', -value => param('username'), -expires => '+7d', -domain => 'webewebin.com' ) ); &header; &member_sec; &footer;
header..
sub header { print qq~ <html> <head> <title>Matrix System</title> <body link="0000FF" vlink="0000FF" alink="0000FF" text="00000"> <font face="arial"> <p> <b> <a href="index.cgi">Home</a> &nbsp; <a href="index.cgi?act=signup">Signup</a> &nbsp; <a href="index.cgi?act=member">Members</a> </b> </p> ~; }
Having this code prints header twice but in the middle of both of them theres a 1, and the cookie isnt working. But if I just remove the cookie part it all looks fine so its something with the cookie

Replies are listed 'Best First'.
Re: Cookies messing up code
by Aristotle (Chancellor) on Oct 30, 2002 at 00:49 UTC

    Simple - the print header(...) calls your own header() function. This function only executes a print, whose return value, 1, is passed back to the calling code, which happens to be a print itself and so outputs that 1.

    Two suggestions: use the object oriented interface to CGI as it's much cleaner, and really do use a templating system to pull all the HTML out of your script. I propose the excellent Template Toolkit 2 module. See also perrin's templating system roundup at Perl.com

    Makeshifts last the longest.

Re: Cookies messing up code
by chromatic (Archbishop) on Oct 30, 2002 at 03:23 UTC

    I assume you're using the CGI module, importing its header() function. Unfortunately, you're defining your own function with the same name. Perl has no way to distinguish the two, and the most-recently-defined function wins, as Aristotle points out. If you had warnings enabled, Perl would have told you that header() was redefined.

Re: Cookies messing up code
by dws (Chancellor) on Oct 30, 2002 at 06:16 UTC
    In addition to the problem identified above, you have two more waiting in queue.
    1. The cookie needs a -path
    2. The -domain is malformed. Try '.webewebin.com'