What am I doing wrong?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
(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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |