in reply to How do you get an "our" var out of an object?

Update: Did this question change? I swear that, when I was replying to it, it implied you wanted to get rid of your "our variables"... (I did just wake up though, so maybe I was fuzzy-headed.)

Upon reading it now, it seems you just want a method which accesses a class variable...

package Foo; our $bar = 'some default'; sub new { bless {} } sub bar { shift; $bar = shift if @_; $bar } package main; my $obj = Foo->new; print $obj->bar, "\n"; $obj->bar('something else'); print $obj->bar, "\n";

Normally people use globals in part to avoid that sort of thing though. This is what you usually see:

package Foo; our $bar = 'some default'; package main; print $Foo::bar, "\n"; $Foo::bar = 'something else'; print $Foo::bar, "\n";

My original answer follows...

I couldn't access it from a reference to the package.

There are a number of ways but they boil down to basically the same thing. You can create a function or functions that will be used to access a lexical variable instead. The simplest is just to create an accessor class method:

package Foo; my $bar = 'some default'; sub bar { $bar = shift if @_; return $bar; }
You could provide a reference directly to the variable...
package Foo; my $bar = 'some default'; sub bar_ref { return \$bar; }
You can create an lvalue sub...
package Foo; my $bar = 'some default'; sub bar : lvalue { $bar; }
Which allows you to use it like this in your main program:
print Foo::bar, "\n"; Foo::bar = 'something else'; print Foo::bar;
You can limit the scope of the variable even further...
package Foo; { my $bar = 'some default'; sub bar : lvalue { $bar } }
So, there are a bunch of ways. The question is really why you want to... There's nothing necessarily bad about globals so long as you use them prudently and don't export them by default. If you have to do some sort of validation when the variable is set, then using the first of the above examples might make sense.

-sauoq
"My two cents aren't worth a dime.";