Click to See Complete Forum and Search --> : Automagic Return Type


rigadon
04-13-2005, 09:11 AM
Hi

Is there some way in which the return type (scalar/array/etc) can be changed depending on what the calling code is expecting? For instance:


# case 1
my @one_two = &config(qw(one two));
print "@one_two\n";

# case 2
my $installation = &config(qw(installation))
print "$installation\n";
exit;

sub config
{
my (@params) = @_;
my %default = (
installation => 'TPG',
one => 'nothing important',
two => 'nothing important again',
);

return map { $default{$_} } @params;
}


The output of which would be:

nothing important nothing important again
1

In case 2 the array is returned as a scalar as that is what the calling code is expecting. Is there some way to determine what is type expected and thus manipulate the config function to return this type?

Thanks
Jay

rigadon
04-13-2005, 09:20 AM
Apologies - I guess all I need to do is this:

my ($installation) = TPG_Functions->config(qw(installation));

Still - if you CAN somehow determine what kind of return type is expected that would be very useful!

CyCo
04-13-2005, 09:29 AM
See wantarray (http://www.perl.com/doc/manual/html/pod/perlfunc/wantarray.html)

rigadon
04-13-2005, 09:44 AM
cracking - cheers!