TheLinuxDuck
01-24-2001, 05:06 PM
I came across this tres qool way to make private data in a perl script. If you define variables inside a block, and also define subs inside that same block, the variables will be private to that block, but the subs will be global. This way, you can alter the variables indirectly, without ever having actual access to them..
I thought it was pretty qool. http://www.linuxnewbie.org/ubb/smile.gif
#!/usr/bin/perl -w
use strict;
use warnings;
#
{
my($userName,@arrayList);
sub changeUserName
{
$userName=$_[0];
}
sub showUserName
{
return $userName;
}
sub addArrayItems
{
push @arrayList, @_;
}
sub showArrayList
{
my($retVal);
$retVal.="$_ " for(@arrayList);
return $retVal;
}
}
#
# Uncomment these two lines to receive a
# compile time error
#
# print "$userName\n";
# print "@arrayList\n";
changeUserName("Jeffrey");
print showUserName()."\n";
addArrayItems("dingle","flumpy","bacteria");
print showArrayList()."\n";
exit;
http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
I thought it was pretty qool. http://www.linuxnewbie.org/ubb/smile.gif
#!/usr/bin/perl -w
use strict;
use warnings;
#
{
my($userName,@arrayList);
sub changeUserName
{
$userName=$_[0];
}
sub showUserName
{
return $userName;
}
sub addArrayItems
{
push @arrayList, @_;
}
sub showArrayList
{
my($retVal);
$retVal.="$_ " for(@arrayList);
return $retVal;
}
}
#
# Uncomment these two lines to receive a
# compile time error
#
# print "$userName\n";
# print "@arrayList\n";
changeUserName("Jeffrey");
print showUserName()."\n";
addArrayItems("dingle","flumpy","bacteria");
print showArrayList()."\n";
exit;
http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq