Re: Storing object references in hashes



Hi Paul,

Many thanks for getting back.

Thanks to your's and other's posts I now realise that my understanding
of scoping at the Package level was wrong. I'd assumed that variables
declared in the main section are 'object level' variables whereas they
are in fact 'class level' variables. Once I'd realised this my problem
was obvious. I think you can see that, with this misconception, I
didn't think the code looked odd

On your others points;

No, I'm not a C programmer. It's much worse that that. For many years I
wrote applications for IBM mainframes using Basic Assembler Language
and the like so I'm well conversant with modules. My problem was
getting into a mind set which was flawed. I'd got into this mind set
because, up to now, I've only used the perl Package capability to break
my code up into discrete logical sections. Because I instantiated each
package only once in an application, it worked. I now realise that
there was no need to instantiate them at all. All I had to do was
reference their methods at the class level.

TRUE/FALSE v's 1/0. Using TRUE/FALSE means more to me than 1/0 so,
until it's demonstarted to me that it's poor programming, I'll
continue. I've used this technique for years, together with ON/OFF etc,
and I'm too old to change now without a good reason. Like the way I lay
out my code, it's a personal preference that suits me and we should all
be allowed those so long as they work.

Use the '=>' notation. Good point. I'll do this in future. It's much
more readable.

Choice of variable names (%people and %persons). Agreed, I'd not
usually do this but it was just a piece of code I'd put together to
demonstrate my problem.

Variable scoping at the lowest possible level. Agreed, but I had a few
problems when I tried to do this as you suggested. The best I could do
is shown in the listing of the current code below. I'd appreciate it if
you could point out any further improvements. (As way of celebration,
I've extended the code to list the names sorted on age.)

Many thanks for your help,

Bryan

* * * * * * * *
* * * * * * * *
use warnings;
use strict;
use constant TRUE => 1;
use constant FALSE => 0;
use Tie::IxHash;
my(%people, %persons);
tie %people, "Tie::IxHash"; # Maintain loading
order
%people = (Bryan => 63, Anne => 62, Kevin => 39, Gareth => 35, Siobhan
=> 31, Kathryn => 28);
print "Unsorted list...........\n";
my($person);
while(my($peopleName, $peopleAge) = each(%people))
{
$person = Person->new($peopleName, $peopleAge); # Get new instance
of Person class and load name and age
$persons{$peopleName} = $person; # New hash
entry with name as key and and new object as value
print "Name: " . $person->getName() . ",\tAge: " . $person->getAge()
.. "\n";
}
print "List sorted on name...........\n";
my($personsName);
foreach $personsName (sort(keys(%persons)))
{
$person = $persons{$personsName}; # Person object
built for this name
print "Name: " . $person->getName() . ",\tAge: " . $person->getAge()
.. "\n";
}
print "List sorted on age...........\n";
foreach $personsName (sort {$people{$a} cmp $people{$b}} keys %people)
{
$person = $persons{$personsName}; # Person object
built for this name
print "Name: " . $person->getName() . ",\tAge: " . $person->getAge()
.. "\n";
}
exit(TRUE);

package Person;

use constant TRUE => 1;
use constant FALSE => 0;

sub new
{
my($className) = shift(@_);
my($self) = {name =>$_[0],
age =>$_[1]};
bless($self, $className);
return($self);
}

sub getName
{
my($self) = shift(@_);
return $self->{name};
}

sub getAge
{
my($self) = shift(@_);
return $self->{age};
}
return(TRUE);

.



Relevant Pages

  • SSIS - Adding Mappings Programatically
    ... I am stuck with Adding Mappings, ... '// Here is subset of my sample code ... Sub SSISPackageDemo() ... Savepackage(pkg, True) '//Save Package to SQL Server ...
    (microsoft.public.sqlserver.dts)
  • Re: best practice ... requires
    ... affect the way Perl parses your program, like strict and warnings). ... Do you know what calling a sub with & does? ... so you can see that my package is nested and I am now wondering how ...
    (comp.lang.perl.misc)
  • Re: perl menubased user interface
    ... actually use a perl package OR a hash for this, In a "perl package" ... sub map_method { ... # and they're all shell commands. ... Do whatever you want to wrap a shell command. ...
    (comp.lang.perl.misc)
  • Re: Using a DBI connection in many places (in the code)
    ... # non-exported package globals go here ... sub set_name { ... nothing to do with DBI. ...
    (comp.lang.perl.modules)
  • Re: multiple packages/classes in one file
    ... > so you can use globals under strict and not just lexicals. ... > this means global to the package that our is used in. ... > sub set_member ... > return $member; ...
    (comp.lang.perl.misc)