I have a string with a package name. Is it possible to get a package variable?

Bad_ptr

in WithVar.pm:

package Test::WithVar;

our @desired_var = qw(1 2 3);

OtherPackage.pm:

package Test::OtherPackage;
use Test::WithVar;

sub test_get_var ($) {
    my $package_name = shift;

    #↓ How to do that ? Is it possible ? ↓
    return @{$package_name::desired_var};
}

# I know that I can access the desired_var with @Test::WithVar::desired_var,
# (explicitly typed package name, a 'bareword')
# but what if I have package name in a string ?

test_get_var('Test::WithVar');

Another solution (i guess) is to define a function in the WithVar package that will return our variable.

package Test::WithVar;

our @desired_var = qw(1 2 3);

sub return_desired_var ($) {
    my $self = shift;
    our @desired_var;
    return \@desired_var;
}

So now I can:

package Test::OtherPackage;
use Test::WithVar;

my $str = 'Test::WithVar';
print @{$str->return_desired_var()}, '\n';

But the problem is the inheritance. I need to inherit from WithVar and child modules will return WithVar::desired_var and I need $ChildPackage::desired_var.

package Test::ChildWithVar; use parent 'Test::WithVar';

our @desired_var = qw(4 5 6); # redefine variable in child module.

And if I write

package Test::OtherPackage;
use Test::ChildWithVar;

my $str = 'Test::ChildWithVar';

# This will print 1 2 3 of course, not 4 5 6.
print @{$str->return_desired_var()}, '\n'; 

So I need somehow to write universal method in Test::WithVar package:

package Test::WithVar;

our @desired_var = qw(1 2 3);

sub return_desired_var_universal ($) {
    my $self = shift;
    my $class = ref $self || $self;
    return $class::desired_var; #Somehow ?!
}

I know that I can use a hash and store package-specific variables in it instead of using 'our' variables. I know. But I need a solution with 'our', or the clear understanding that this is not possible.

Thanks.

nlu

You should use strict, for many reasons, the one concerning your case being explained here.

You can disable the warning, use strict emits for the following code by

# some code block
{  
   ...
   no strict 'refs'; 
   return @{$package_name.'::desired_var'};
}

You should try to put what you really want to achieve in a seperate question and explain why you think, it could only be done with our variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I have a string with a package name. Is it possible to get a package variable?

From Dev

Do I have to run apt-get remove package_name before apt-get purge package_name?

From Dev

Is it possible to get application name when package is removed

From Dev

Is it possible to have 2 classes with the same name and in the same package?

From Dev

How do I get the real package name and version for a pseudo package?

From Dev

variable "name of package" in teamcity

From Dev

Get APK package name

From Dev

Android Is It possible to get cache directory path with package name?

From Dev

Do I always have to type package name in Java?

From Dev

Get package by variable

From Dev

On Fedora, why do I have to install a devel package to get javac?

From Dev

I have changed package name of the project but when i am creating apk it shows the package name different in android studio

From Dev

How to get a Kotlin KClass from a package class name string?

From Dev

Is there any way I can get a package name from terminal?

From Dev

find - I need to get the name of the package from a list of RPM

From Dev

How to get final package name?

From Dev

java get name of package of class

From Dev

How to get package name in script?

From Dev

How to get package name in script?

From Dev

Unable to get package name in appium

From Dev

Get current activity name and package?

From Dev

Is it possible to specify package name dynamically during build?

From Dev

Is it possible don't specify package name?

From Dev

Node Package - Is it possible to interpolate a variable in package.json?

From Dev

Get app name knowing the name of the package

From Dev

How to get the name of the running app, not the package name?

From Dev

Is it possible to have a Debian package pre-install script run apt-get commands?

From Dev

How to get the variable name of a button I have just clicked, tkinter

From Dev

Access Perl variable with it's package name in scalar

Related Related

  1. 1

    I have a string with a package name. Is it possible to get a package variable?

  2. 2

    Do I have to run apt-get remove package_name before apt-get purge package_name?

  3. 3

    Is it possible to get application name when package is removed

  4. 4

    Is it possible to have 2 classes with the same name and in the same package?

  5. 5

    How do I get the real package name and version for a pseudo package?

  6. 6

    variable "name of package" in teamcity

  7. 7

    Get APK package name

  8. 8

    Android Is It possible to get cache directory path with package name?

  9. 9

    Do I always have to type package name in Java?

  10. 10

    Get package by variable

  11. 11

    On Fedora, why do I have to install a devel package to get javac?

  12. 12

    I have changed package name of the project but when i am creating apk it shows the package name different in android studio

  13. 13

    How to get a Kotlin KClass from a package class name string?

  14. 14

    Is there any way I can get a package name from terminal?

  15. 15

    find - I need to get the name of the package from a list of RPM

  16. 16

    How to get final package name?

  17. 17

    java get name of package of class

  18. 18

    How to get package name in script?

  19. 19

    How to get package name in script?

  20. 20

    Unable to get package name in appium

  21. 21

    Get current activity name and package?

  22. 22

    Is it possible to specify package name dynamically during build?

  23. 23

    Is it possible don't specify package name?

  24. 24

    Node Package - Is it possible to interpolate a variable in package.json?

  25. 25

    Get app name knowing the name of the package

  26. 26

    How to get the name of the running app, not the package name?

  27. 27

    Is it possible to have a Debian package pre-install script run apt-get commands?

  28. 28

    How to get the variable name of a button I have just clicked, tkinter

  29. 29

    Access Perl variable with it's package name in scalar

HotTag

Archive