Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Prototypes/Parameters to a Function/Sub-Routine

Reply
Thread Tools

Prototypes/Parameters to a Function/Sub-Routine

 
 
O. Olson
Guest
Posts: n/a
 
      08-03-2007
On Aug 1, 10:01 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
>
> Perhaps you'll find the Params::Util module from CPAN helpful for
> checking parameters.
>

Thanks Steven for pointing this out. I would try this out as soon as I
get the time.
O.O.

 
Reply With Quote
 
 
 
 
O. Olson
Guest
Posts: n/a
 
      08-03-2007
On Aug 2, 1:50 pm, (E-Mail Removed)-berlin.de wrote:
>
> It is usually an indication of a design error if a method has to
> care about the class an object comes from. If an object *can* invoke
> a method (through inheritance, for instance) the method should be able
> to handle it.
>
> Anno



Thanks Anno.
Consider if I have to compare two objects the same class. Something
like:

$obj1->diff($obj2)

Where both $obj1 and $obj2 belong to the same class and the function
diff() calculates the difference between them.

In this case diff() needs to be able to ensure that the object passed
as a parameter is of the same common class to be able to calculate the
difference.

O.O.

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-03-2007
O. Olson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Thanks Paul for your detailed discussion.
>
> On Aug 1, 6:49 pm, Paul Lalli <(E-Mail Removed)> wrote:
> > What part of perlsub gave you the idea that was valid syntax?
> >

>
> No, I did not mean to say that I got this idea from perlsub - just
> that I wanted to do something like this - but this was not available
> in Perlsub. Anyway now I got the idea.
>
> > UNIVERSAL::isa($obj, "MyClass") or
> > croak "Argument to printObject is not a MyClass";

>
>
> I think this is what I wanted i.e. a way to check if an object is of a
> certain class.


If you know that $obj *is* an object, that's better written as

$obj->isa( 'MyClass');

Anno
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-03-2007
O. Olson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On Aug 2, 1:50 pm, (E-Mail Removed)-berlin.de wrote:
> >
> > It is usually an indication of a design error if a method has to
> > care about the class an object comes from. If an object *can* invoke
> > a method (through inheritance, for instance) the method should be able
> > to handle it.
> >
> > Anno

>
>
> Thanks Anno.
> Consider if I have to compare two objects the same class. Something
> like:
>
> $obj1->diff($obj2)
>
> Where both $obj1 and $obj2 belong to the same class and the function
> diff() calculates the difference between them.
>
> In this case diff() needs to be able to ensure that the object passed
> as a parameter is of the same common class to be able to calculate the
> difference.


Not necessarily. A user could present $obj2 coming from a subclass,
in which case the test may fail, depending how it it done.

In a purely OO design, the diff method would use accessor methods of
the common class to compare values, not rely on a particular structure
of the objects.

Anno
 
Reply With Quote
 
O. Olson
Guest
Posts: n/a
 
      08-04-2007
On Aug 3, 2:03 pm, (E-Mail Removed)-berlin.de wrote:
> Not necessarily. A user could present $obj2 coming from a subclass,
> in which case the test may fail, depending how it it done.
>
> In a purely OO design, the diff method would use accessor methods of
> the common class to compare values, not rely on a particular structure
> of the objects.
>
> Anno


Dear Anno,

Thanks for taking interest - But I'm still not entirely clear what
you mean. ( Let's not consider inheritance for a while, because it may
complicate matters - and I'm not using it right now).

Consider for example the following class

-----------------------------------------------------
package MyTime;

sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "Too many arguments\n" unless @_ <=3;
my $self;

if ( @_ == 0 ) {

my @time = localtime( );
$self = { HOUR => $time[ 2 ],
MINUTE => $time[ 1 ],
SECOND => $time[ 0 ] };
} else {
$self = { HOUR => $_[ 0 ],
MINUTE => $_[ 1 ] || 0,
SECOND => $_[ 2 ] || 0 };
}

return bless $self, $class;
}

sub timeDiff {

my $start_time = shift; # Start Time - First argument
my $stop_time = shift; # Stop Time - Second argument


print "Calculating the Time Difference between: " . $start_time-
>as_text . " to " . $stop_time->as_text; #print "\n";


my $start_seconds = $start_time->{HOUR}*3600 + $start_time-
>{MINUTE}*60 + $start_time->{SECOND};

my $stop_seconds = $stop_time->{HOUR}*3600 + $stop_time-
>{MINUTE}*60 + $stop_time->{SECOND};


my $diff_seconds = $stop_seconds - $start_seconds;

print " = $diff_seconds \n";

return $diff_seconds;

}

-----------------------------------------------------

Assuming timeDiff() is called as:

$time_start-> timeDiff($time_stop)

-----------------------------------------------------


My question is how can I ensure that the second argument passed to
timeDiff() is of The same class MyTime.

Thanks a lot,
O.O.

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-04-2007
O. Olson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> On Aug 3, 2:03 pm, (E-Mail Removed)-berlin.de wrote:
> > Not necessarily. A user could present $obj2 coming from a subclass,
> > in which case the test may fail, depending how it it done.
> >
> > In a purely OO design, the diff method would use accessor methods of
> > the common class to compare values, not rely on a particular structure
> > of the objects.
> >
> > Anno

>
> Dear Anno,
>
> Thanks for taking interest - But I'm still not entirely clear what
> you mean. ( Let's not consider inheritance for a while, because it may
> complicate matters - and I'm not using it right now).
>
> Consider for example the following class


There are ready-made time classes on CPAN that do time calculations.
In a real application I would urge you to use one of those. I'll
assume your implementation of MyTime is an exercise.

> -----------------------------------------------------
> package MyTime;
>
> sub new {
> my $inv = shift;
> my $class = ref( $inv ) || $inv;
> die "Too many arguments\n" unless @_ <=3;


You mean ">=".

> my $self;
>
> if ( @_ == 0 ) {
>
> my @time = localtime( );
> $self = { HOUR => $time[ 2 ],
> MINUTE => $time[ 1 ],
> SECOND => $time[ 0 ] };
> } else {
> $self = { HOUR => $_[ 0 ],
> MINUTE => $_[ 1 ] || 0,
> SECOND => $_[ 2 ] || 0 };
> }
>
> return bless $self, $class;
> }
>
> sub timeDiff {
>
> my $start_time = shift; # Start Time - First argument
> my $stop_time = shift; # Stop Time - Second argument
>
>
> print "Calculating the Time Difference between: " . $start_time-
> >as_text . " to " . $stop_time->as_text; #print "\n";

>
> my $start_seconds = $start_time->{HOUR}*3600 + $start_time-
> >{MINUTE}*60 + $start_time->{SECOND};

> my $stop_seconds = $stop_time->{HOUR}*3600 + $stop_time-
> >{MINUTE}*60 + $stop_time->{SECOND};

>
> my $diff_seconds = $stop_seconds - $start_seconds;
>
> print " = $diff_seconds \n";
>
> return $diff_seconds;
>
> }
>
> -----------------------------------------------------
>
> Assuming timeDiff() is called as:
>
> $time_start-> timeDiff($time_stop)
>
> -----------------------------------------------------
>
>
> My question is how can I ensure that the second argument passed to
> timeDiff() is of The same class MyTime.


You could say

die "..." unless $time_stop->isa( 'MyTime);

but I simply wouldn't bother.

If someone calls ->timeDiff with an inappropriate argument, access to
->{ HOUR} will fail and you'll see warnings about using an undefined
value. (Your *are* running with warnings and strict, aren't you?)
That should be enough to detect and correct the error.

On the other hand, you are duplicating the code to transform a
time value to seconds in your timeDiff method. Instead, define
a method (code untested)

sub inSecs {
my $time =shift;
($time->{ HOUR}*60 + $time->{ MINUTE})*60 + $time->{ SECOND};
}

and base timeDiff on that:

sub timeDiff {
my ( $start, $stop) = @_;
$stop->inSecs - $start->inSecs;
}

Besides the better code re-use, this makes sure that a call with a
non-time will fail because the ->inSecs method won't be defined.

Anno
 
Reply With Quote
 
O. Olson
Guest
Posts: n/a
 
      08-04-2007
Dear Anno,

Thank you for your explanations - I think this is what I wanted. If
the have any more doubts, I would post them to another thread as they
would be unrelated to this topic.

Thanks again,
O.O.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments