how can i get a variable inside a package


{ 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


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:

use strict;
use warnings;

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

print $example::baz;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.