Доступ к переменным класса с помощью переменной с именем класса в perl

I'm wondering how I would go about doing this:

package Something;
our $secret = "blah";

sub get_secret {
    my ($class) = @_;
    return; # I want to return the secret variable here
}

Now when I go

print Something->get_secret();

I want it to print blah. Now before you tell me to just use $secret, I want to make sure that if a derived class uses Something as base, and I call get_secret I should get that class' secret.

How do you reference the package variable using $class? I know I can use eval but is there more elegant solution?

5
задан Bill the Lizard 15 October 2017 в 16:37
поделиться