Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

is it ok to name object methods after core functions?

by leocharre (Priest)
on Aug 02, 2007 at 15:12 UTC ( #630297=perlquestion: print w/ replies, xml ) Need Help??
leocharre has asked for the wisdom of the Perl Monks concerning the following question:

Having a subroutine named 'log' or 'exists' would be silly. What about object method names, though? Is there potential for trouble there?

Like $o->open()

Comment on is it ok to name object methods after core functions?
Re: is it ok to name object methods after core functions?
by citromatik (Curate) on Aug 02, 2007 at 15:17 UTC

    They are safe because in OO you don't export them, so the methods of a given class belongs to the class they are declared in.

    citromatik

Re: is it ok to name object methods after core functions?
by ikegami (Pope) on Aug 02, 2007 at 15:19 UTC
    It works.
    >perl -e"sub print { shift; CORE::print(qq{[@_]\n}); } __PACKAGE__->pr +int('foo'); [foo]

    Whether it will lead to confusion or not is up to you.

Re: is it ok to name object methods after core functions?
by Fletch (Chancellor) on Aug 02, 2007 at 15:29 UTC

    I'd be wary of potential gotchas if you ever used indirect object notation (but then you shouldn't so it's not a big issue). The specific case I'm thinking of would be open called as a package method open MyModule "blah"; I may just be barking up the wrong tree but I want to say that's going to get CORE::open. (Someone who's not as lazy as I am at the moment might actually check what happens . . . :).

Re: is it ok to name object methods after core functions?
by archfool (Monk) on Aug 02, 2007 at 15:50 UTC
    Should be fine. Modules like IO::File and IO::Handle have ->open methods. :)
Re: is it ok to name object methods after core functions?
by dsheroh (Parson) on Aug 02, 2007 at 18:05 UTC
    With the way that Perl OO magic works, when you do
    my $o = Some::Class->new; $o->open;
    the $o->open turns into Some::Class::open($o) - by calling it as a method, you're effectively prefixing a namespace to it, so it will never collide with anything outside that namespace and is, therefore, safe. (Unless your class is named 'main', I suppose, but you really, really shouldn't be doing that anyhow.)
Re: is it ok to name object methods after core functions?
by Joost (Canon) on Aug 02, 2007 at 20:29 UTC
Re: is it ok to name object methods after core functions?
by codeacrobat (Chaplain) on Aug 03, 2007 at 06:50 UTC
      Also you should keep in mind that Perl::Critic will complain about builtin function overwrites

      It's "may complain" not "will complain". P::C is configurable in all things.

      I love P::C, but changing code just to make a comment disappear isn't what it's about. It highlights things that may be problems. Change them if it makes your code better. If <code>$door->open</open> is more intention revealing than the alternatives leave it alone.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://630297]
Approved by ikegami
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-07-13 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls