Here's some sample scripts so it's clearer what I'm attempting to do & how everything works:

Login script:
#!c:/apache/perl/bin/perl.exe -wT use CGI; use strict; use CGI::Session; use HTML::Template; use MyAppCommon; my $foo = new CGI; my $alias = $foo->param('alias'); my $userID = 123; #normally would be pulled from DB, but keeping the c +ode as simple as possible for testing purposes my $empty; my $session = new CGI::Session(undef, $empty, {Directory=>"c:/apache/s +essions"}); $session->expire('+18h'); $session->param("id", $userID); $session->param("alias", $alias); my $cookie = $foo->cookie(-name => 'main', -value => $session->id, -expires => '+18h', -path => '/'); print $foo->header(-cookie => $cookie); MyAppCommon::topPublic(); my $template=HTML::Template->new(filename=>"c:/apache/templates/main/l +ogin.tmpl"); $template->param(ALIAS => $alias); print $template->output; MyAppCommon::bottomPublic($alias);

One of the many pages that contain very similar coding (a page that simply wants to check if the user is logged in or not, and does NOT want to log them in if they're not... ie. does NOT create a session if they're not logged in... it only wants to check if they are & retrieve relevant session variables if they are logged in:

#!c:/apache/perl/bin/perl.exe -wT use CGI; use strict; use CGI::Session; use HTML::Template; use MyAppCommon; my $foo = new CGI; print $foo->header(); my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses +sions'}); if ( $session->is_new() ) { $session->delete(); } MyAppCommon::topPublic(); my $template=HTML::Template->new(filename=>"c:/apache/templates/main/h +ome.tmpl"); print $template->output; MyAppCommon::bottomPublic($alias, $userAccess, $membership);

IF I comment out the following code of the above script:
my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache/ses +sions'}); if ( $session->is_new() ) { $session->delete(); }

and replace it with:
&test; sub test { my $sid = cookie('main') || undef; my @sessionInfo; my $session = new CGI::Session(undef, $sid, {Directory=>'c:/apache +/sessions'}); if ( $session->is_new() ) { $session->delete(); } return 1; }
I get the error I mentioned.

" on every page, you check whether there's a session logged in or not # depending whether there is one, you do whatever you need to do"

This is exactly what I want to do. Using the sessions ID, I want to determine if one exists with that session ID and to *NOT* create one if it doesn't exist.

In reply to Re^3: Code works fine, but in subroutine does not... help plz! by Stenyj
in thread Code works fine, but in subroutine does not... help plz! by Stenyj

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.