Solved

Variable protection

Posted on 2002-04-26
4
199 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

679 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