Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^6: Use method/function signatures with Perl

by Ovid (Cardinal)
on Dec 06, 2004 at 16:35 UTC ( #412689=note: print w/replies, xml ) Need Help??

in reply to Re^5: Use method/function signatures with Perl
in thread Use method/function signatures with Perl

Now I understand what you're asking. You're actually dealing with some interesting stuff here, but I think that most of the concern is a theoretical non-issue, but in practice it is an issue, but I think we're looking at it slightly differently. For example, your initial code doesn't work:

sub foo(ARRAY $bar) {...} # later foo(Bar->new);

That breaks immediately because &foo is expecting an ARRAY, not an object of class Bar. It's an easy fix:

sub foo(Bar $bar) {...}

Now it doesn't matter how someone implements Bar internally. However, there is one (false) objection and one very real objection. The false objection is this:

sub foo(Bar $bar) { print $bar->[3]; }

Obviously, violating encapsulation is going to cause problems regardless of whether or not method signatures are implemented. The real problem, however, and I think this is what you're getting at, lies in this:

my $parser = HTML::TokeParser->new(\$html); sub foo(ARRAY $token) {...} foo($parser->get_token) {...}

HTML::TokeParser returns array references. However, what if someone wants to use HTML::TokeParser::Simple? That module blesses the references. As a result, it is no longer a drop-in replacement. I am forced to alter the above code. (And this just highlighted a bug in my code that I'll have to fix ASAP -- darn.)


New address of my CGI Course.

Replies are listed 'Best First'.
Re^7: Use method/function signatures with Perl
by dragonchild (Archbishop) on Dec 06, 2004 at 16:40 UTC
    That breaks immediately because &foo is expecting an ARRAY, not an object of class Bar.

    In other words, because you're (mis)using ref (which we agreed you shouldn't), you already implement the scheme I suggested. However, there's going to be the user (and it's usually someone like me ... :-) that would assume that since I'm using an array to represent my Bar class, it should fit into either an ARRAY or Bar hole.

    It may not be a code issue as much as a documentation issue. That said, there is still an issue. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://412689]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-10-16 12:12 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (85 votes). Check out past polls.