There appears to be a new pragma named parent
that does roughly the same thing as base
. What does parent
do that warrants a new (non-core) module? I am missing something?
2 Answers
base
tried to do one too many things – automatically handling loading modules but also allowing establishing inheritance from classes already loaded (possibly from a file whose name wasn't based on the module name). To sort of make it work, there was some hackery that caused surprising results in some cases. Rather than break backwards compatibility, a new, replacement pragma parent
was introduced with cleaner semantics.
parent
will be a core module as of 5.10.1.
Update: forgot that base
handles fields
(if you are using the fields
pragma), which parent
doesn't do.
-
24Also, compare perl -wE'say require base' to perl -wE'say require parent'– ysthMay 19, 2009 at 6:22
-
I happen to come back to this page to look up the answer over and over again. Mnemonic:
base
andparent
appeared in alphabetical order. Jan 11, 2019 at 14:51 -
-
1@cat looks like it was changed in 2016. metacpan.org/diff/file?target=CORION/parent-0.235/…– ysthApr 29, 2019 at 4:47
-
1@cat
> perl5.14.1 -wE 'say require parent' All your base are belong to us > perl5.14.1 -wE 'say require base' 1
Jun 26, 2019 at 17:19
Armed with the extra bit of information from ysth, I was able to see the differences in the docs:
The base
pragma does the following things:
- adds the named package to
@ISA
- loads the module with the same name as the named package using
require
(unless it detects that the package has already been loaded) - won't fail if a module with the same name as the package doesn't exist
- dies if there are no symbols in the named package
- if
$VERSION
does not exist in named package, base sets it to"-1, set by base.pm"
- initializes the
fields
of the named package if they exist - does not call the import function of the named package
The parent
pragma does the following things:
- adds the named package to
@ISA
- loads the module with the same name as the named package using
require
- accepts an option that tells it not to die if a module with the same name as the package doesn't exist
-
4Almost. The norequire option just tells it not to do the require (so the class must already be loaded). This means it doesn't have to trap errors and decide whether they should be suppressed or not.– ysthMay 19, 2009 at 6:17