Subject: [LON-CAPA-dev] A lesson in globals

From: Guy Albertelli II <[email-masked]>
Date: Fri, 21 Mar 2003 17:37:33 -0500 (EST)

I noticed some things that I wanted to blather on about

The sumamry is 

'Don't declare a global variable using "my", things will act wacky'

if you declare a global using my it becomes global to the file meaning
it can be talked about with out namespace throuhgt a file. (And I
don't know of a way to access it from another file)

ie:
----- start test.pl -----
package globaltest;
use strict;
my $silly='a';
print '1'.$silly."\n";
print '2'.$globaltest::silly."\n";

package notglobaltest;
print '3'.$silly."\n";
print '4'.$globaltest::silly."\n";
----- end test.pl -----

Note that $globaltest::silly never works and $silly always works

If you want to declare a variable that is global to a package and want
to be able to refer to it without using the full package reference you
need to do 'use vars'

----- start test.pl -----
package globaltest;
use strict;
use vars qw($silly);
$silly='a';
print '1'.$silly."\n";
print '2'.$globaltest::silly."\n";

package notglobaltest;
# the line below will fail to compile 
# print '3'.$silly."\n";
print '4'.$globaltest::silly."\n";
----- end test.pl -----

Now the global is properly enclosed inside of the globaltest package
and when inside notglobaltest I need to specify the package reference
to get access to it.

If you want to set a global and plan to always refer to it by it's
fullname, you don't need to anything special:
----- start test.pl -----
package globaltest;
use strict;
$globaltest::silly='a';
#the line below will fail to compile
#print '1'.$silly."\n";
print '2'.$globaltest::silly."\n";

package notglobaltest;
# the line below will fail to compile
# print '3'.$silly."\n";
print '4'.$globaltest::silly."\n";
----- end test.pl -----

Now only the full variable references work.

-- 
[email-masked]  LON-CAPA Developer  0-7-7-1-

---

From: Gerd Kortemeyer <[email-masked]>
Date: Fri, 21 Mar 2003 18:02:58 -0500

That's all true, but what if you want the following:

* variable (like a tied hash) should NOT be available from outside the
package
* inside the package, you want to refer to it just by its name

Looks like "my $silly" as a package global works great for that.

Guy Albertelli II wrote:

> _______________________________________________
> LON-CAPA-dev mailing list
> [email-masked]
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-dev

---

From: Guy Albertelli II <[email-masked]>
Date: Fri, 21 Mar 2003 19:30:57 -0500 (EST)

Hi Gerd,

> Looks like "my $silly" as a package global works great for that.

No it doesn't.

> > print '4'.$globaltest::silly."\n";

$silly is available in both pacakages.

But it's scope is limited to this file.

---

From: "H. K. Ng" <[email-masked]>
Date: Mon, 24 Mar 2003 16:13:55 -0500

Is there a complete localauth.pm that shows how to do that local 
authentication? I need to implement one for FSU.

Thanks,
hk

---

From: Guy Albertelli II <[email-masked]>
Date: Mon, 24 Mar 2003 16:42:49 -0500 (EST)

Hi Hon-Kie,

> authentication? I need to implement one for FSU.

this is what is in use at Simon Fraser University:

# The LON-CAPA dummy localauthentication mechnism
# 8/24 Guy Albertelli

package localauth;

# ----START LOCAL CHANGES HERE ----- DON'T DELETE THIS LINE
use strict;
use ckid;
sub localauth {
  my ($uname,$upass,$args)= @_;
  my ($correct,$msg)=&ckid::unsecurecheckid($uname,$upass,1,0);
  #unsecurecheckid is backwards from us.
  if ($correct eq 0) { return 1; }
  return 0;
}
# ----END LOCAL CHANGES HERE ----- DON'T DELETE THIS LINE

1;
__END__

lond is expecting &localauth::localauth to return a 1 if the use is
authenticated and 0 if the user fails to be authenticated

$uname is the user name $upass is the password in plain text, $args is
whatever follows the : in the users passwd file.