Solved

Variable protection

Posted on 2002-04-26
4
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

636 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