Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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?

Cheers,
Ovid

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2014-11-27 08:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (182 votes), past polls