Ovid has asked for the wisdom of the Perl Monks concerning the following question:

A little OO style question has been bugging me for a while. Consider HTML::TokeParser::Simple. Currently, if you want to alter attributes of start tags, you can do this:

$token ->delete_attr('foo') ->set_attr( bar => 'baz' ) if $token->is_start_tag;

Some appear to argue that the "is_start_tag" test is superfluous. Basically, mvc argued that having the test is irrelevent. If the object can do what I want, let it do it, otherwise it ignores the message. So how does that work out if I want to clean up all references to font tags in a HTML document?

while(my $token = $parser->get_token) { $token->del_attr('font'); print $token->as_is; }

Right now to do that, I have to do the following:

while(my $token = $parser->get_token) { $token->del_attr('font') if $token->is_start_tag; print $token->as_is; }

Currently, calling set_attr() or del_attr() on anything but a start tag will croak(). (That was a mistake that I will fix. It should carp and move on.) Other than that, how do you feel about the two styles above? My concern with the former style is the possibility a bug in the code leading someone to accidentally try to set attributes on something other than start tags and missing the start tags themselves. In other words, if the code doesn't alert you to the appropriateness of the message, how do you know if the right objects are getting the right messages?

My concern with the latter stems from adding extra tests in the code. The more code, the more bugs. Which style would you choose and why?


New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)