Update2 Ignore this first suggestion(See ikegami's comment below)
Shouldnt that be
our $references;
instead of "my $references;" ?

Update1:
How about:

my @references; # -- inside new() push @references, $objref; # #.. inside sub test() .. # foreach my $object(@references){ ....}

Update 3:
Since I fuffled previous responses, I spent some time getting this thing working (and also understanding how perl OO works). The working code below may help the OP. The idea for the "Team" package is based on Moron's(++) post, below.

#!/usr/bin/perl use strict; use warnings; package User; # our @references; sub new { my ($class, %arg) = @_; my $self = { _name => $arg{name} || "unknown" ,_team => $arg{team} || "unknown" }; bless $self, $class; #push @references, $self; my ($team) = Team->find($arg{team}) ; $arg{team} and ( $team ||= Team->new($arg{team}) ); $team and $team->AddUser($self); return $self; } sub name{ # Accessor $_[0]->{_name}; } sub List_Opponents { my $self = shift @_; my $myTeam = Team->find( $self->{_team} ); foreach my $T (Team->GetTeamList()) { next if $self->{_team} eq $T->{name}; #find opposing team #do something to $T print " " . $self->name . "\ton $self->{_team} vs. " . "\tmembers of $T->{name} \n"; } } sub getUserByName{ my ($self, $lookfor) = @_; my @Users; foreach my $t (Team->GetTeamList()) { # next if $self->{_team} eq $t->{name}; push @Users, grep {$_->name eq $lookfor} $t->GetUserList; } return @Users } ######################################## package Team; my @TeamList; sub new{ my ($class, $name) = @_; my $self = {name=>$name}; bless $self, $class; push @TeamList, $self; return $self; } sub AddUser{ #By Object my $self = shift; while (local $_=shift){ # $_ contains the NAME of the user to + add my $userTeam= findUserObj ($self, $_); # Look for This userobj if ($userTeam){ $userTeam == $self and next; # is already there in this lis +t. dont add again warn "*** User " . $_->name . " is already in team $userTea +m->{name}. Now also added to $self->{name}\n"; } push @{$self->{userlist}},$_; } } sub CreateAndAddUser{ my ($self, @UserNames) = @_; AddUser($self, map {User->new(name => $_, team=> $self->{name})} + @UserNames ); } sub GetUserList{ my $self = shift; return () unless exists $self->{userlist} ; return ( @{$self->{userlist}} ); } sub find{ #Return Team object (or undef) given team NAME. my $self = shift; my $lookfor = shift || $self; # In case CLASS method is called. for my $t(@TeamList){ return $t if $t->{name} eq $lookfor; } return undef; } sub findUserObj{ my ($self, $UserObj) = @_; for my $t (@TeamList){ return $t if grep {$_ == $UserObj} GetUserList($t) ; } return undef; } sub GetTeamList{ return @TeamList; } ############################################## package main; my $t1 = Team->new ("MyTeam"); my $t2 = Team->new ("OtherTeam"); my $SingleUser = User->new(name => "OneSingleUser", team=> "teamBeta") +; my @testUsers = ( (map { User->new(name => "user$_", team=> "teamalpha")} 0..4), (map { User->new(name => "user$_", team=> "teamBeta") } 5..9) ); #Cread Anon Team object (which we can find later) Team->new("Planets")->CreateAndAddUser( qw /mercury venus earth mars +jupiter /); $t1->AddUser (@testUsers[0,1,2] ); $t2->AddUser (@testUsers[3,5,6] ); $t1->CreateAndAddUser( qw /foo bar baz quux /); $t2->CreateAndAddUser( qw /Ahmed sohrab rustom quasim /); Team->find("Planets")->CreateAndAddUser( qw /saturn Asteroids pluto/) +; $testUsers[0]->List_Opponents(); $testUsers[5]->List_Opponents(); $SingleUser->List_Opponents(); for my $t (Team->GetTeamList()){ print "---Team: $t->{name} -----\n"; print " " . $_->name . "\n" for $t->GetUserList; }

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken


In reply to Re: Perl OO - Class Data by NetWallah
in thread Perl OO - Class Data by yoda54

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.