I tend not to use "our" vars, but rather other ways to get the job done. Today I thought I'd give our vars a spin, and couldn't get perl to do what I wanted it to do. I could get the "our" var to print when I used the package name, but I couldn't access it from a reference to the package.
use strict; use warnings; package foo; # this is how I usually do it. use fields qw(bar); # can't accomplish what I want with our vars alone. #As the answerers explained, I need a helper function to do this. # I guess using fields is a way to get the same effect with a, what do + you call it, syntactic artificial sweetener... our $bar = "10\n"; no warnings 'deprecated'; #otherwise get warnings because fields uses +pseudohash sub new { defined( my $self = shift) or die "no object"; $self = fields::new($self); $self->{bar} = 10; return $self; } package main; my $foo_obj = foo->new(); #print '$fooobj::bar: ' . $foo_obj::bar . "\n"; # I want this to prin +t 10, but it's an uninitialized var. So commented out. print '$foo_obj-{bar}: ' . $foo_obj->{bar} . "\n"; #this prings 10 usi +ng fields print '$foo::bar: ' . $foo::bar . "\n"; # this prints 10 without the p +ackage name
What am I doing wrong?

(This was prompted by musing on my answer to arunvelusamy's question earlier today at [ooperl] calling reference to a annonymous sub)

UPDATE: Well, as the ever-helpful BUU and sauoqhave pointed out, it appears this was a fairly hare brained idea to begin with. FWIW, I have updated the above code to also use fields, which is what I customarily do to accomplish this task, as neither of the answerers mentioned this particular way of getting the mission accomplished, and I rather like fields.

UPDATE 2: In answer to sauoq, no, my question didn't change. The only change was I integrated use fields just now. Thanks again for putting up with my harebrainedness ;)


In reply to 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.