in reply to how to code a class and use it with hash of hash as data member?
The touched-up version of your code follows. Please note that I expect that there are some serious problems with it yet, as I haven't done this before. However, it will at least run with my test program.#!/usr/bin/perl -w use strict; use myObject; my $T = myObject->new(); # We add a server by passing in the server name, and a hash ref of the # credentials we want to add $T->addServer('barrabas', { password=>"sa", hostname=>"devo.marcorp.or +g" }); $T->printMe; $T->deleteServer('default'); $T->printMe; $T->removeServerMap; $T->printMe;
--roboticuspackage myObject; $VERSION = 1.00; use strict; use warnings; use diagnostics; use vars qw(@ISA); use Carp (); $Carp::Verbose =1; { my %serverMap = ( default => { ipaddress => "127.0.0.1", dsn => "default", userid => "sa", password => "password", instance => "instance" } ); sub new { my ($slf, %args) = @_; my %array=(); %array = (%args); #? my $self={}; return bless $self; } sub printMe() { my ($self) = @_; print "------ Available Servers ------\n"; for my $server (keys %serverMap) { print "\nServer: ", $server, "\n"; my $hr = $serverMap{$server}; for my $cr (keys %{$serverMap{$server}}) { print "\t$cr => $serverMap{$server}{$c +r}\n"; } } } sub addServer() { my ($self, $srvName, $hrCreds) = @_; $serverMap{$srvName} = $hrCreds; } sub removeServerMap($) { my ($self) = @_; %serverMap = (); } sub deleteServer($){ my ($self, $srvName) = @_; delete $serverMap{$srvName}; } } 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to code a class and use it with hash of hash as data member?
by chromatic (Archbishop) on Mar 17, 2006 at 08:38 UTC | |
by edwardt_tril (Sexton) on Mar 17, 2006 at 15:57 UTC | |
by chromatic (Archbishop) on Mar 17, 2006 at 19:50 UTC | |
by edwardt_tril (Sexton) on Mar 17, 2006 at 20:17 UTC | |
by edwardt_tril (Sexton) on Mar 21, 2006 at 08:09 UTC |