$html = function();Within this function, I am checking the values in a hash that is a global variable (declared with 'our'). Values in this hash are assigned by a method call. I don't like this set up and I want to change things.
However one thing that I can't do is this:
$html = $self->function;The function must remain a function and not become a method.
What could I do within the function, so that I am not checking the value of a global variable that is altered by a separate method?
{
my %value_for;
sub set_values {
my ( $key, $value ) = shift;
$value_for{ $key } = $value;
}
sub function {
# ...
my $special_value = $value_for{specialness};
# ...
}
}
So value_for is shared by just those two subs.
Phil
Sounds like a good place to have a Class::Singleton instance to hold shared configuration information between the different modules.
You've said what you don't want to do; what is it you do want to do? I mean, I could just say "delete the code where you're checking the global variable", but I suspect wouldn't be acceptable. It would probably help if you show some sample code illustrating the current situation, how the variable is being altered and being checked.
Perhaps one possibility is to pass the relevant hash members into the function as parameters. So that, if you code currently looks like
function();
sub function {
print $hash{'status'};
}
you could change it to
function( $hash{'status'} );
sub function {
my( $status ) = @_;
print $status;
}
perlmonks.org content © perlmonks.org and Anonymous Monk, Fletch, jdporter, philcrow
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03