The rwo code blocks above do exactly that.

No they do not prove anything, you did not show us where that code was, how it was called, nor did you call it in your example.

In the code you just showed us there are many reasons those code segments can be skipped leaving $LoggedOn_user_id set to its initial 0

regarding $LoggedOn_user_id being undef

at

# $sid = '7032f2c7f5a2c721a483dc75fc29595e'; $session = new CGI::Session("driver:MySQL", $sid, {Handle=>$dbh});
you dont check to see if $session->id() ne $sid. If they are not the same it is because either the $sid never existed or that session has expired. in either of those cases a new session-collection and session-id is created and when you try to use my $username1 = $session->param("user_id"); to set $LoggedOn_user_id = $username1; it will be undef as the following code demonstrates.
#!/usr/bin/perl use strict; use warnings; select STDOUT; $| = 1; use CGI; use CGI::Session; my $existn=0; my $sid=make_first(); sleep 10; find_if_exist ($sid); sleep 20; find_if_exist ($sid); find_if_exist('abcd'); exit; sub make_first { my $session = new CGI::Session(undef, undef, {Directory=>'.'}); my $sid=$session->id; $session->expires('+15s'); $session->param('user_id',time); $session->flush(); return $sid; } sub find_if_exist { my $sid0=shift; $existn++; print '******Exist call:'.$existn."\n"; my $session = new CGI::Session(undef, $sid0, {Directory=>'.'}); my $sid=$session->id; my $user=$session->param('user_id'); unless (defined $user) {$user='undef-as-string'; } unless ($sid0 eq $sid) {print "********different "; } print 'sid0:'.$sid0.' sid:'.$sid."\n"; print 'user:'.$user."\n\n"; }
result
******Exist call:1 sid0:2bd4a7c8d0001e49e97dc332d6bf619e sid:2bd4a7c8d0001e49e97dc332d6bf +619e user:1491953482 ******Exist call:2 ********different sid0:2bd4a7c8d0001e49e97dc332d6bf619e sid:bcd0c394 +67ce09c5b5be9910f9e1798b user:undef-as-string ******Exist call:3 ********different sid0:abcd sid:2dd5bc5bc5e297150b1e4c7f58166135 user:undef-as-string


In reply to Re^20: global var by huck
in thread global var by tultalk

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.