Solved

Variable protection

Posted on 2002-04-26
4
196 Views
Last Modified: 2010-05-18
I read somewhere that you can/should protect variables from being accessed outside of scope.  say I have the following code.
#!/opt/perl/bin/perl -w

$scriptname = $0;
$scriptname =~ s/.*\///;

use FileHandle;
require "/home/oliver/perl/dev/lib.pm";

umask 0;
&init_logfile;
&current_logfile_name;
$logfile_name = "Death_to_Sanity";
print "$logfile_name\n";
&current_logfile_name;


-------library.pm----------
sub init_logfile {
    $logfile_name = $scriptname . "." . $$ . ".log";

    local $ret_val;
    $ret_val = sysopen(ERROR_LOG, $logfile_name, O_WRONLY|O_CREAT, 0666);
    if ( $ret_val ) {
        varify_variable( "Start Time: `date` ", $debug );
    } else {
        die("Can't open Log file $logfile_name !");
    }
}

sub varify_variable {
    local $test_string = shift;
    local $log_level = shift;

    print ERROR_LOG "$test_string\n";
    print "$test_string\n" if $log_level;
}
sub current_logfile_name {
    print STDERR "Current Value $logfile_name\n";
}
1;


------OutPut------
example
Current Value example.5923.log
Death_to_Sanity
Current Value Death_to_Sanity
 
------Desired result------

What I would like to happen is either the $logfile_name variable to be local to the "lib.pm" file and only be modifiable by functions of that lib or to have an error kick off if the value has been changed.
0
Comment
Question by:SafeSql
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
rj2 earned 75 total points
ID: 6972322
my $logfile_name;
0
 

Author Comment

by:SafeSql
ID: 6972381
That means that the developer would have to know that they can't declare the variable global.  If they stomp on one of my variables the script could be in trouble.  I want the pm file variables to be called but separated from the calling script. see the difference?  If I put the my in the .pm file it is lost after the call is finished.

0
 
LVL 10

Expert Comment

by:rj2
ID: 6972871
use my, but move the declarion to the top of library.pm
0
 
LVL 8

Expert Comment

by:inq123
ID: 9491244
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept a comment by rj2

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

inq123
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now