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.";

In reply to Re: How do you get an "our" var out of an object? by sauoq
in thread How do you get an "our" var out of an object? by tphyahoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.