edward--

I've never done any object-oriented Perl before, so please bear with me. I've been meaning to teach myself OOPerl, so I thought I'd give it a shot.

Anyway, I've cleaned up some syntax errors in your code, and munged some stuff around, and came up with something that seems to work. I'm using a trivial 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;
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.

When some guru comes by and notices something, please be kind... ;^)
package 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
--roboticus

In reply to Re: how to code a class and use it with hash of hash as data member? by roboticus
in thread how to code a class and use it with hash of hash as data member? by edwardt_tril

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.