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.
| [reply] [Watch: Dir/Any] [d/l] |
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 ),
);
| [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] |
|
perl -dea
DB<1> m main
via UNIVERSAL: DOES
via UNIVERSAL: VERSION
via UNIVERSAL: can
via UNIVERSAL: isa
DB<2> q
| [reply] [Watch: Dir/Any] [d/l] |
|
|
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:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] |
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
| [reply] [Watch: Dir/Any] |
|
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":)
| [reply] [Watch: Dir/Any] |
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
| [reply] [Watch: Dir/Any] |
|
Did you try this? Just execute the following script
sub BEGIN {
warn "BEGIN called\n";
}
| [reply] [Watch: Dir/Any] [d/l] |
|
Yeah, my mistake. I did try it actually, I just misread the results!
-sam
| [reply] [Watch: Dir/Any] |
|
|