Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Which are perl-internal subroutines?

by Tobiwan (Beadle)
on Nov 18, 2007 at 01:16 UTC ( [id://651473]=perlquestion: print w/replies, xml ) Need Help??

Tobiwan has asked for the wisdom of the Perl Monks concerning the following question:

I've to identify any "perl-internal" subroutine in a class to avoid to redefine something, which has side effects. At the actual point I know this methods:
  • import
  • unimport
  • can
  • isa
  • BEGIN
  • CHECK
  • INIT
  • END
  • DESTROY
  • AUTOLOAD
  • MODIFY_(CODE|SCALAR|ARRAY|HASH|GLOB)_ATTRIBUTES
  • FETCH_(CODE|SCALAR|ARRAY|HASH|GLOB)_ATTRIBUTES
Is this the complete list?

Replies are listed 'Best First'.
Re: Which are perl-internal subroutines?
by Somni (Friar) on Nov 18, 2007 at 02:18 UTC
    perldoc UNIVERSAL will help; for example, you missed VERSION.

    I realize samtregar mentioned BEGIN, CHECK, INIT, and END as not being subroutines. That is, in fact, not the case. They are all special subroutines, and attempting to define one by that name will cause it to trigger as if you had left out the "sub" keyword. So, you were on the right track with them.

    package Foo; sub BEGIN { print "I am Foo, called in BEGIN!\n" }
    The print statement is run even if BEGIN is not called. You can't even call BEGIN yourself after that.

    Personally I'd avoid any subroutine name in all caps. For example, you missed all of the tie hooks (see perltie). I have seen modules (Class::InsideOut springs to mind) that will call all-caps subroutines in your modules, but it's chancy; the subroutine name may get hijacked by Perl at some point.

Re: Which are perl-internal subroutines?
by diotalevi (Canon) on Nov 18, 2007 at 02:33 UTC

    I compiled a list once. This changes over time of course. You won't be able to expect to write a list once and then not update it as new things show up on CPAN or as perl changes.

    # A dictionary of stuff that can show up in UNIVERSAL. our @UNIVERSAL_METHODS = ( # core perl qw( isa can VERSION ), # core perl 5.9.4+ 'DOES', # UNIVERSAL.pm 'import', # UNIVERSAL/require.pm qw( require use ), # UNIVERSAL/dump.pm qw( blessed dump peek refaddr ), # UNIVERSAL/exports.pm 'exports', # UNIVERSAL/moniker.pm qw( moniker plural_moniker ), # UNIVERSAL/which.pm 'which', # SUPER.pm qw( super SUPER ), );

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Thanks a lot. I'll check these methods. I don't need the all-time-complete-list of that subroutines, but as more I can identify as better it is.
      Hi,

      I've check all your described methods. All these methods are polluted by modules which are not needed or automatical loaded. So I only add the VERSION-subroutine to my list.

      I don't want to check the attributes for all methods, other modules provide. Only for methods which are provided by pure perl.

        The list for core perl is everything from UNIVERSAL.pm and up. SUPER is also in the core and is frequently used. The other things are still somewhat uncommon. You would be wrong to think that only 'VERSION' is the available method. When I load my perl up with no modules at all and look at in the debugger I get four methods.

        perl -dea DB<1> m main via UNIVERSAL: DOES via UNIVERSAL: VERSION via UNIVERSAL: can via UNIVERSAL: isa DB<2> q

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Which are perl-internal subroutines?
by dragonchild (Archbishop) on Nov 18, 2007 at 02:11 UTC
    Out of curiousity, what are you attempting to accomplish? I'm asking this because this kind of solution smells like a solution to an XY problem. If you explain what you're attempting to do, we might be able to suggest a simpler solution to the actual problem.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      There are three reasons for my question:
      first:
      curiosity
      second:
      I've seen people to become desperate with the definition of a import() method. If yuo dan't know about it, you wander, why your method is called "statical". I want to avoid things like that.
      third:
      I write an attribute-extension for subroutines and I want to check, if you define an attribute to an perl-internal method, that this can cause problems. It's the automatic way for the second reason.
Re: Which are perl-internal subroutines?
by GrandFather (Saint) on Nov 18, 2007 at 02:35 UTC

    In general if you avoid bare word identifiers that are either all caps or all lowercase you will not collide with "Perl-internal" identifiers.


    Perl is environmentally friendly - it saves trees
      Sorry, but that's not a sollution I can use. There exist a code-styleguide in my company. Methods are lowercase with underscores and constands are uppercase (which are solved as subroutines in module "use constants":)
Re: Which are perl-internal subroutines?
by samtregar (Abbot) on Nov 18, 2007 at 01:52 UTC
    BEGIN, CHECK, INIT and END are not subroutines, they're blocks. You can have a subroutine named any of these and it won't cause any problems.

    -sam

      Did you try this? Just execute the following script
      sub BEGIN { warn "BEGIN called\n"; }
        Yeah, my mistake. I did try it actually, I just misread the results!

        -sam

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found