returning several types from sub

returning several types from sub

am 15.12.2006 10:52:46 von extern.Lars.Oeschey

Hi,

I'm in the process of cleaning a badly written old program of mine, using
"strict" and putting everything in subs. Now I have a problem, that I don't
get values back from a sub as I expect. The code looks something like this
(everything would be too much to paste):

I fill several variables like this:

my ($printer,$printermodel,%ladestellenhash,@abteilungen,@spedi tionen,
@bearbeiter,@versandarten,$hostname,$type)=ReadIni();

where ReadIni looks like this:

sub ReadIni {
my (%ladestellenhash,$printer,$printermodel);
.....
my @ladestellen=$cfg->Parameters("ladestellen");
foreach (@ladestellen) {
$ladestellenhash{$_}=$cfg->val("ladestellen",$_);
}
....
return
($printer,$printermodel,%ladestellenhash,@abteilungen,@spedi tionen,
@bearbeiter,@versandarten,$hostname,$type);
}

so I read several values from a .ini file and fill the variables with it.
What I *do* get from the return() is $printer and $printermodel.
Am I using something wrong with returning arrays and hashes?

regards, Lars
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: returning several types from sub

am 15.12.2006 11:08:36 von kenneth

> Am I using something wrong with returning arrays and hashes?

Yes - the arrays/hashes you attempt to return will be 'unrolled' into
the *single* list that will be returned, which is not what you want. A
simple fix is to return references to the arrays/hashes instead, i.e.
something like:

return ($someScalar, $anotherScalar, \%someHash, \@someArray,
$yetAnotherScalar)

Of course, on the returning side you have to treat the references as
references:

my ($a, $b, $hashRef, $arrayRef, $c) = ...

$hashRef->{aKey} ... (versus $hash{aKey})
$arrayRef->[0] ... (versus $array[0])

Or unroll them directly...

my %realHash = %$hashRef;

etc...

HTH,

ken1

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: returning several types from sub

am 15.12.2006 11:10:31 von Stuart Arnold

Yup!
You need to pass back refs and receive the refs.
Ie:
my ($printer,$printermodel,$rh_ladestellenhash, $ra_abteilungen,
$ra_speditionen,
$ra_bearbeiter,$ra_versandarten,$hostname,$type)=ReadIni();
And then deref(if so desired)
%ladestellenhash = %{$rh_ladestellenhash}
And so on.

Your return should be:
Return
($printer,$printermodel,\%ladestellenhash,\@abteilungen,\@sp editionen,
\@bearbeiter,\@versandarten,$hostname,$type);


-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
extern.Lars.Oeschey@AUDI.DE
Sent: Friday, December 15, 2006 4:53 AM
To: activeperl@listserv.ActiveState.com
Subject: returning several types from sub


Hi,

I'm in the process of cleaning a badly written old program of mine, using
"strict" and putting everything in subs. Now I have a problem, that I don't
get values back from a sub as I expect. The code looks something like this
(everything would be too much to paste):

I fill several variables like this:

my ($printer,$printermodel,%ladestellenhash,@abteilungen,@spedi tionen,
@bearbeiter,@versandarten,$hostname,$type)=ReadIni();

where ReadIni looks like this:

sub ReadIni {
my (%ladestellenhash,$printer,$printermodel);
.....
my @ladestellen=$cfg->Parameters("ladestellen");
foreach (@ladestellen) {
$ladestellenhash{$_}=$cfg->val("ladestellen",$_);
}
....
return
($printer,$printermodel,%ladestellenhash,@abteilungen,@spedi tionen,
@bearbeiter,@versandarten,$hostname,$type);
}

so I read several values from a .ini file and fill the variables with it.
What I *do* get from the return() is $printer and $printermodel.
Am I using something wrong with returning arrays and hashes?

regards, Lars
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Loading a COM object in Perl

am 15.12.2006 11:25:00 von Foo JH

Hi all,

First I'd like to share this little gem I found on the web: Cuba, a COM
object for TAPI.
http://software.techrepublic.com.com/download.aspx?&compid=5 1135&docid=274060

Ok, now to address my problem: I'd like to load this COM object in Perl.
The key line looks like this:

Win32::OLE::Warn = 3;
my $cuba = Win32::OLE->new('CUBALib.Phone');

Unfortunately it does not work. I get the following message:
Win32::OLE(0.1707) error 0x800401f3: "Invalid class string"

I suspect I can do it somehow, because in the OLE browser that comes
with ActivePerl, it did find the COM objects and all its methods. I just
don't know what I am doing wrong. This library can bring TAPI to the
Perl community...for free!

Hope to get some help here...


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: returning several types from sub

am 15.12.2006 13:23:04 von extern.Lars.Oeschey

> You need to pass back refs and receive the refs.

thanks a lot for both answers, solved it now.

Lars
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs