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

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;
Be a part of the DaniWeb community

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