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

How do you return a Hash from a perl dll created by perl Ctrl? Been using dlls for months in production, return of a numeric or text works fine. TypeLib Snippet
Methods => { 'New' => { RetType => VT_DISPATCH, TotalParams => 1, NumOptionalParams => 0, ParamList => [ cm => VT_DISPATCH ] }, 'readDirectory' => {RetType => VT_VARIANT, TotalParams => 1, NumOptionalParams => 0, ParamList => [ dir => VT_BS +TR ] }, }, # end of 'Methods'
snippet of dll code.
sub readDirectory { my $self = shift; my %rlist; $rlist{"file1"} = '1'; $rlist{"file1"} = '2'; return \%rlist; }
Calling program $Lista returns "Win32::OLE"
my $Lista = $obj->readDirectory("txt"); %List = %$Lista; ???????????????????????????

Replies are listed 'Best First'.
Re: Return hash from a com object. ?
by kcott (Archbishop) on Jun 11, 2016 at 05:00 UTC

    G'day cdlvj,

    Disclaimer: I can't help you with specifics of "perl dll created by perl Ctrl"; sounds like a MSWin thing (with which I'm unfamiliar).

    In many functions, you'll only be interested in the hash's keys and values; the name of &readDirectory suggests it would fall into this category. In these cases, your best choice will probably be:

    return { %rlist };
    • There are no references to %rlist when it goes out of scope at the end of &readDirectory: cleanup can occur.
    • All callers have a hash which is decoupled from all other callers' hashes and the original %rlist.

    In other functions, particularly those that need to modify the hash in some way, a reference to the original hash may be important. In these cases, your best choice will probably be:

    return \%rlist;
    • There is one reference to %rlist when it goes out of scope at the end of &readDirectory: cleanup cannot occur.
    • All callers have a reference to the same hash; any of which could be modifying the hash in some way, at any time, without notice.

    See also: perlreftut, perldsc and perlref.

    — Ken

Re: Return hash from a com object. ?
by Anonymous Monk on Jun 11, 2016 at 01:21 UTC