I want to write an OO module such that the objects will "carry" user-supplied subs. Now I want these subs to be allowed to give error and warning messages based on info stored in the objects themselves, so that they will have to "communicate" in some way or the other with them.

The most logical solution that occurred to me would be of passing the object itself as a first argument to the called sub (this would be part of the UI of the module), so that the latter could call a suitable method on it.

Any alternative suggestion? How is this kind of things generally done?

An oversimplified, and somewhat ridicoulous, but hopefully illustrative example of what I meant above is as follows:

#!/usr/bin/perl use strict; use warnings; package Foo::Parse; sub new { my ($self, $name, $action)=@_; bless { _name => $name, _action => $action }, $self; } sub parse { my $self=shift; $self->{_action}->($self, @_); } sub warn { my ($self,@msg)=@_; warn "[$self->{_name}] @msg\n"; } package main; my $fp=Foo::Parse->new( 'FooParser', sub { my $self=shift; local $_=shift; s/^\w+:// or $self->warn("something wrong!"); split /,/; } ); $fp->parse($_) while <>; __END__

In reply to Seeking advice for OO-related strategy by blazar

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.