First off, your sub type method. That prototype is, as I recall, useless. Which, in my opinion, is worse than normal prototypes in that it's promising a certain behaviour which it can't fulfill (thus it's misleading). Due to the way that prototypes (compile-time checking) and object methods (run-time lookup and resolution) work together, that prototype will get ignored. This is evidenced by the fact that you're actually passing in a scalar (the $_ in $_->type()) rather than calling it as type(%$_). You're best off using prototypes much like goto - avoid it unless you know why you need it.

The next item I'll address is the fact you're modifying Event.pm, which I presume is part of the Linux::Inotify distribution. This is great if your intention is to submit it as a patch to the author. Otherwise, next time you upgrade your Linux::Inotify installation, or next time you go to a new box, you'll end up without your patch. This will lead to annoying bugs - in this case, they probably won't be hard to find, but they probably will be hard to figure out why it worked before and suddenly stopped when you upgraded or moved to a new machine unless you happen to remember every single patch you've made.

Better would be to put it in your code like this:

{ package Linux::Inotify::Event; sub type { shift->{map} } }
That is, it's better only if either you aren't going to submit a patch to the author or if the author isn't being responsive.

As for your core questions, you're right that there probably should be accessor methods (but not attribute-setting methods - this seems to be a read-only object here). The author in this case has elected not to provide them and implicitly says that you should access them directly when he says "An Linux::Inotify::Event object has some interesting data members: mask, cookie and name." Thus Torsten seems to be saying that you should just use "$_->{mask}" - no need for a function.

As there are no access restrictions in perl (except via closures), whatever you can do in sub type, you can do outside of that sub.

Good luck!


In reply to Re: Linux::Inotify usage. by Tanktalus
in thread Linux::Inotify usage. by Hue-Bond

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!
  • 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, details, 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, summary, 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.