this may be a little bit long, but im trying to demonstrate something that i can't figure why it's happening - when you try to load a session before a sub that also loads the session, it won't be able to store it for next page load! Why?

to demonstrate :
test1.pl will generate a session id and move to test2.pl
test2.pl will initially try to display any messages that were added. On first load, you should see it says no messages are found.
When you click on "refresh page", you will see "TESTING 1 2 3 SHOULD SHOW".
If you comment off MARKING 1 to 3 and try to start from test1.pl to test2.pl again, once "refresh page" is clicked, no messages are shown!

library.pl is the file that is imported from test2.pl


test1.pl :
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use CGI::Session qw/-ip-match/; require "library.pl"; # instantiate a new CGI object my $cgi = new CGI; my $sessionid; my $session = new CGI::Session(); my $sessionid = $session->id(); print<<OUTPUT; <html> <body> <br><br> <form action="test2.pl" METHOD="POST"> <input type="hidden" name="sessionid" value="$sessionid"> <input type="submit" value="Generate Session"> </form> </body> </html> OUTPUT print xxx;
test2.pl
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use CGI::Session qw/-ip-match/; require "library.pl"; # instantiate a new CGI object my $cgi = new CGI; my $sessionid = $cgi->param("sessionid"); my $msg = $cgi->param("message"); print<<OUTPUT; <html> <body> OUTPUT #display any added messages if any &DisplayMessage; # PERCULIAR - remove these and you won't see the "TESTING 1 2 3 SH +OULD SHOW" anymore! how to retain? # MARKING 1 # my $session = CGI::Session->load($sessionid); # MARKING 2 # $session->param("otherThings", "......."); # MARKING 3 #TO TEST .. THIS IS WHERE MESSAGE IS ADDED &AddMessage("TESTING 1 2 3 SHOULD SHOW\n\n"); print<<OUTPUT; <br><br> <form action="test2.pl" METHOD="POST"> <input type="hidden" name="sessionid" value="$sessionid"> <input type="submit" value="Refresh Page"> </form> </body> </html> OUTPUT print xxx;
library.pl :
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use CGI::Session qw/-ip-match/; # instantiate a new CGI object my $cgi = new CGI; my $sessionid = $cgi->param("sessionid"); sub AddMessage { my ($text)=@_; #open up existing session and add message my $session = CGI::Session->load($sessionid); $session->param("message", $text); print "added a new message.\n"; } sub DisplayMessage{ #prints the message out from previous saved session my $session = CGI::Session->load($sessionid); if($session->param("message")){ print $session->param("message"); }else{ print "no messages found.\n"; } } return 1;

In reply to need serious help with CGI::Session by adrive

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.