On Monday, April 15, 2002, at 02:40 , Ryan wrote:
> --At 4/15/02 4:25 PM -0500, PK Eidesis wrote:
>> sub makeCalendar {
>> $type = @_;
>> if($type eq "month") {
>> print "all the code for a monthly calendar";
>> elsif($type eq "week") {
>> print "all the code for a weekly calendar";
>> elsif($type eq "year") {
>> print "all the code for a yearly calendar";
>> }
>> }
>
> Personally, I'd change all those prints to returns -
We agree that they should be 'returns' but why not
the usual sort of predef approach
sub makeCalendar {
my ($type) = @_;
my $retString = ''; # safety set
if($type eq "month") { $retString = "all the code for a monthly calendar"
;
} elsif($type eq "week") { $retString = "all the code for a weekly
calendar";
} elsif($type eq "year") { $retString = "all the code for a yearly
calendar"; }
$retString ; # the default value would be returned on error case
}
Assuming that the branch tree is built in the order of
expected usage - or one might go with the fact that the
hash is the core of the perl reality space - and one should
have a reasonable expectation that it will be kosher:
sub doCal {
my ($type) = @_;
my %retHash = (
'month' => 'all the code for a monthly calendar',
'week' => 'all the code for a weekly calendar',
'year' => 'all the code for a yearly calendar',
);
$retHash{ $type }; # defaults to empty if not used
} # end doCal
Since this is when the player starts thinking in terms of:
sub doMon { my $string = "all Your Months Be Mine"; }
sub doWeek {my $string = "all Your Weeks Be Mine";}
sub doYear { my $string = "this is the year line";}
sub doCal {
my ($type) = @_;
my %retHash = (
'month' => \&doMon,
'week' => \&doWeek ,
'year' => \&doYear ,
);
no strict 'refs'; # must skank around the ref indirection here
my $retString = $retHash{ $type }->()
if ( $retHash{$type } ); # if we have it
$retString ; # return what ever the function passed
} # end doCal
cf:
http://www.wetware.com/drieux/CS/lang/Perl/Beginners/HashSwitch.txt
ciao
drieux
---