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, [at] abteilungen, [at] spedi tionen,
[at] bearbeiter, [at] versandarten,$hostname,$type)=ReadIni();

where ReadIni looks like this:

sub ReadIni {
my (%ladestellenhash,$printer,$printermodel);
.....
my [at] ladestellen=$cfg->Parameters("ladestellen");
foreach ( [at] ladestellen) {
$ladestellenhash{$_}=$cfg->val("ladestellen",$_);
}
....
return
($printer,$printermodel,%ladestellenhash, [at] abteilungen, [at] spedi tionen,
[at] bearbeiter, [at] 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 [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
extern.Lars.Oeschey [ Fr, 15 Dezember 2006 10:52 ] [ ID #1569511 ]

Re: returning several types from sub

> 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, \ [at] 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 [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
kenneth [ Fr, 15 Dezember 2006 11:08 ] [ ID #1569512 ]

RE: returning several types from sub

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,\ [at] abteilungen,\ [at] sp editionen,
\ [at] bearbeiter,\ [at] versandarten,$hostname,$type);


-----Original Message-----
From: activeperl-bounces [at] listserv.ActiveState.com
[mailto:activeperl-bounces [at] listserv.ActiveState.com] On Behalf Of
extern.Lars.Oeschey [at] AUDI.DE
Sent: Friday, December 15, 2006 4:53 AM
To: activeperl [at] 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, [at] abteilungen, [at] spedi tionen,
[at] bearbeiter, [at] versandarten,$hostname,$type)=ReadIni();

where ReadIni looks like this:

sub ReadIni {
my (%ladestellenhash,$printer,$printermodel);
.....
my [at] ladestellen=$cfg->Parameters("ladestellen");
foreach ( [at] ladestellen) {
$ladestellenhash{$_}=$cfg->val("ladestellen",$_);
}
....
return
($printer,$printermodel,%ladestellenhash, [at] abteilungen, [at] spedi tionen,
[at] bearbeiter, [at] 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 [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Stuart Arnold [ Fr, 15 Dezember 2006 11:10 ] [ ID #1569513 ]

Loading a COM object in Perl

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 [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Foo JH [ Fr, 15 Dezember 2006 11:25 ] [ ID #1569514 ]

RE: returning several types from sub

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

thanks a lot for both answers, solved it now.

Lars
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
extern.Lars.Oeschey [ Fr, 15 Dezember 2006 13:23 ] [ ID #1569515 ]
Perl » gmane.comp.lang.perl.active-perl » returning several types from sub

Vorheriges Thema: untie error (from JComboBox?)
Nächstes Thema: binary (?) to string