0

how can i get a variable inside a package

example:

{ package example;
    my $baz = "sometext";
    sub new{
        my $self = shift;
        bless \$self;
    }
}

my $foo = example->new;

how can I get the value of $baz i tried

print $foo->{vv};
print $foo::vv

and no luck

thanks

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by d5e5
0

You have declared $baz with the my function which limits the scope of $baz to the block in which it is declared thus making it impossible to obtain its value from outside that block. A my declares the listed variables to be local (lexically) to the enclosing block...

In the following script I changed the my to our and deleted the statements not necessary for defining $baz and referring to its value from outside the package:

#!/usr/bin/perl
use strict;
use warnings;

{ package example;
    our $baz = "sometext";
}

print $example::baz;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.