Hazael Maldonado Torres wrote:
>
>
> I am trying to use prototype functions with object but this seems not
> to work, I mean perl does not check for the type of arguments.
>
> I usually do this.
>
> sub array_print(\@);
>
> ..
> ...
>
> sub array_print(\@)
> {
> # my code
> }
>
>
> Then when I call it as:
>
> array_print(@this_array);
>
> Perl is supposed to check the prototype and change the array by its
> reference. Now I try to put this same behavior but with object, and
> then when I call the same function as:
>
> $obj_array->array_print(@this_array);
>
> Perl sends the complete array to the function and not the reference.
> So I need to do this
>
> $obj_array->array_print([EMAIL PROTECTED]);
>
> Does any one knows how to force perl to behave as normal?
There is no way of prototyping object methods as run-time inheritance
means that Perl cannot know which subroutine will provide the
method until it is actually called.
Perl isn't C, and it's hardly ever a good idea to prototype
Perl subroutines. It's far more useful to be able to pass an array,
an array or hash slice, or a list of scalars (or a mixture of these)
to a subroutine that simply expects a LIST.
If you must, you can code
sub array_print {
my $array = shift;
die unless ref $array eq 'ARRAY';
}
but I would expect something like 'array_print' to look at
least something like the built-in 'print' operator which
takes a list of values.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>