Hi,

I could really do with a hand on some OO perl that I'm battling with please perlmonks.

Here is what I'm trying to do: I have 2 packages

LogSimple DEC::Project

The LogSimple logging object is created like so, the important piece to focus on here in this object is the function reference stored in 'exit'

my $log = LogSimple->new( logdir => $logdir, logfile => $logfile, loglevel => $loglevel, verbosity => $verbosity, exit => \&DEC::Project::exit_routine );

Next I initialise the following object, there can be multiples of these with different serial numbers.

my $server = DEC::Project->new( serial => '17353', log => $log, );

The LogSimple object handles any exit from the project like this:

$log->err("Something went wrong",1);

Where the script would exit 1. I use the LogSimple package in other projects and have recently added the optional ability to make a callback so that 'stuff' can take place before the exit..

Now, when something goes wrong talking to $server object I would like to call the subroutine

\&DEC::Project::exit_routine

I can get exit_routine to execute except I want to be able to issue commands to all of the $server objects from \&DEC::Project::exit_routine just before the exit.

I am trying to store references to all of the $server objects as they are created in ->new inside a global hash like this:

$server_objects{$options{'serial'}} = \$self;

My thinking is that I should be able to loop through the hash executing whatever methods I need against all of the objects like this:

sub exit_routine { for my $serial ( keys %server_objects ) { my $server = $server_objects{$serial}; my @ret = $server->logout(); } } sub logout { my $self = shift; print "logging out"; } };

However I keep running into the following: Can't call method "logout" on unblessed reference

This is my first real venture into OO programming so this is a real challenge for me. Anyone able to offer advice ?

Many thanks, Darren


In reply to Storing object references during creation of ->new object by perldarren

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.