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
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