Solved

Variable protection

Posted on 2002-04-26
4
198 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

839 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