mutagen has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl class that expects an ID as an arg to it's new() method, does a database query with that ID, and offers a bunch of getX methods to get the various pieces of data from the database query. So for example, this works as expected:
use DocumentInfo; my $doc_info = DocumentInfo::new('919'); my $last_accessed = $doc_info->getLastAccessed();
OK, great. but I don't want to make new DocumentInfo objects directly. I have a DocumentManager class that has a method getDocumentInfo, which passes its arguments to the new() method of DocumentInfo, and which I want to return a DocumentInfo object.. Here is that method in DocumentManager:
use DocumentInfo; ... sub getDocumentInfo { my $file_id = shift; # suspect problem here return DocumentInfo::new($file_id); }
and here's how I attempt to call it:
use DocumentManager; my $doc_mgr = DocumentManager::new(); my $doc_info = $doc_mgr->getDocumentInfo('919');
It doesn't work because DocumentInfo does not get a scalar '919' as an arg to it's new() method - it gets a hash reference - $doc_id. Is this OO idiom impossible in Perl, or is there some syntax that will work?

Replies are listed 'Best First'.
Re: object method that returns object
by japhy (Canon) on Apr 13, 2002 at 01:43 UTC
    The first argument to an object method is the object.
    sub getDocumentInfo { my ($object, $file_id) = @_; # ... }
    Your code was not making an adjustment for that.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: object method that returns object
by trs80 (Priest) on Apr 13, 2002 at 01:46 UTC
    You aren't removing the object from the parameters being passed.
    sub getDocumentInfo { my ($self,$file_id) = @_; # suspect problem here return DocumentInfo::new($file_id); }
Re: object method that returns object
by Fletch (Bishop) on Apr 13, 2002 at 02:02 UTC

    Not speaking to the actual problem that's already been answered, but you might want to check out POOP (that's Perl Object-Oriented Persistence; get your mind out of the gutter :) for an existing class rather than trying to reinvent the wheel (especially considering you've got problems with fundamental parts of OOP in perl).

Re: object method that returns object
by thpfft (Chaplain) on Apr 13, 2002 at 02:30 UTC

    I think the code you're looking for is

    use Class::DBI;

    But then i always think that, and it is true that it requires a fair bit of familiarity with oop to get it going properly: you have to subclass it, and soon after that build a separate factory class it for it to really work, for example. So I would humbly suggest that you use the good advice above to persevere with your version until it works kind of and you're acquainted with the many difficulties involved in making it work fully: then you'll _really_ enjoy doing the same thing in twenty minutes with Class::DBI. That's what i did, and you can see what kind of craven evangelist I've become.

Re: object method that returns object
by rinceWind (Monsignor) on Apr 13, 2002 at 10:00 UTC
    mutagen, another point:

    You should be using a method call to new, not a straight subroutine call, as it is (or ought to be!) a class method.

    my $doc_info = DocumentInfo->new('919');
    This passes the class name as $_[0], and will allow inherited constructors to work properly. See perldoc perlboot, perltoot and perlobj for more about how Perl does OO