function overloading in Perl

W

wana

I read something about function/method overloading in Perl relating to
OOP, but I was wondering about how to do it normally.

here's the code:

sub SaveToFile
#takes array reference and file name as argument
#and saves array contents to file
{
my ($array, $filename) = @_;
open my $file, '>', $filename or die "Couldn't open $filename:
$!";
print $file @{$array};
close $file or die "Error closing $filename: $!";
}
sub StringToFile
#like SaveToFile but takes string instead of array reference
{
my ($string, $filename) = @_;
open my $file, '>', $filename or die "Couldn't open $filename:
$!";
print $file $string;
close $file or die "Error closing $filename: $!";
}

As you can see, I have two functions that basically do the same thing.

In C++ I would give them the same name and the parameter list would
differentiate the two (function overloading). In Perl, there is no
parameter list as far as I have learned.

What would be the best way to incorporate these functions into one
function if overloading is not possible?

I mean so it can be called as SaveToFile(\@myarray,$filename) or
SaveToFile('my string',$filename) with the appropriate results.

Thanks!

wana
 
J

John Bokma

wana said:
I mean so it can be called as SaveToFile(\@myarray,$filename) or
SaveToFile('my string',$filename) with the appropriate results.

check if your first argument is a ref or not.
 
M

Michael Carman

wana said:
I read something about function/method overloading in Perl relating to
OOP, but I was wondering about how to do it normally.

In Perl OO different classes can have methods with the same name that do
different things, but I don't consider that overloading.
I have two functions that basically do the same thing.

In C++ I would give them the same name and the parameter list would
differentiate the two (function overloading). In Perl, there is no
parameter list as far as I have learned.

Of course there's a parameter list, that's how we pass arguments. What
there is not is strong typing and compile-time type checking that would
allow perl to resolve the ambiguity between two subs with the same name.
So you can't overload functions in Perl.
What would be the best way to incorporate these functions into one
function if overloading is not possible?

I mean so it can be called as SaveToFile(\@myarray,$filename) or
SaveToFile('my string',$filename) with the appropriate results.

Perl doesn't have strong typing, but it does distinguish between three
basic data types: scalars, arrays, and hashes. You can use this to check
the reference type of the first argument, e.g.

sub SaveToFile {
if (! ref($_[0])) {
# process data as string
}
elsif (ref($_[0]) eq 'ARRAY') {
# process data as array
}
else {
# error: unexpected ref type
}
}

-mjc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top