Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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)

In reply to OO style question: how much information to hide? by Ovid

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and nobody stirs...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (3)
    As of 2017-11-25 03:08 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (355 votes). Check out past polls.