in reply to Another Reason RTFMing Is A Good Thing

Note that you would have gotten the same problems if you wrote it without state:
my $hs = HTML::Strip->new(); sub clean_html { ... }
Personally, I wouldn't have (mis)used state for such an optimization (nor would I have written in the pre-5.10 style). You want to start parsing from a clean state, and as such, the code should reflect this. Your code doesn't.

Replies are listed 'Best First'.
Re^2: Another Reason RTFMing Is A Good Thing
by Limbic~Region (Chancellor) on Oct 19, 2009 at 16:01 UTC
    JavaFan,
    Yes, it would have produced the same unexpected results but I would have also had to move:
    my $hs = HTML::Strip->new();
    higher in the file since I prefer to list all my subs at the bottom rather than the top of my file.

    I am not sure I understand what you mean by (mis)using state. Is it not intended to ensure a variable is only initialized once? My expectation was that it was parsing from a clean state - primarily because I hadn't properly RTFMd - I didn't realize $hs was maintaining state.

    I guess what you are saying is that in the end, after I realized what was going on I should have made the code more clear. I think adding a comment would suffice:

    $hs->eof(); # ensure each call to parse() is from a clean slate

    That is in fact what I intend and what the module offers to allow it. If I have completely misunderstood you, please clarify. If you just disagree then I note your objection but do not agree with your position.

    Cheers - L~R

      I am not sure I understand what you mean by (mis)using state. Is it not intended to ensure a variable is only initialized once?
      Yes, in order to keep state. But your code obviously does not want to keep state. Just because you can call $hs->eof doesn't mean I would do that. Is HTML::Strip->new such an expensive call compared to $hs->eof that it's worthwhile to avoid calling it?
        JavaFan,
        Thank you. I now clearly see your point. Since this code is being written for a client who wants to understand all of the code, I will make it more clear. The client has also expressed a desire for optimizing for run time but I can show him how Benchmark works and let them decide for themselves (I suspect my way is marginally faster). Thanks again.

        Cheers - L~R