Solved

Variable protection

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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.…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

20 Experts available now in Live!

Get 1:1 Help Now