Home > Archive > PERL Miscellaneous > November 2005 > FAQ 7.25 How can I find out my current package?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
FAQ 7.25 How can I find out my current package?
|
|
| PerlFAQ Server 2005-11-24, 3:58 am |
| This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
7.25: How can I find out my current package?
If you're just a random program, you can do this to find out what the
currently compiled package is:
my $packname = __PACKAGE__;
But, if you're a method and you want to print an error message that
includes the kind of object you were called on (which is not necessarily
the same as the one in which you were compiled):
sub amethod {
my $self = shift;
my $class = ref($self) || $self;
warn "called me from a $class object";
}
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
| |
| John Bokma 2005-11-24, 3:58 am |
| PerlFAQ Server <comdog@pair.com> wrote:
> sub amethod {
> my $self = shift;
> my $class = ref($self) || $self;
> warn "called me from a $class object";
> }
Foo::amethod;
is that a Foo object....
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
| |
| brian d foy 2005-11-24, 6:57 pm |
| In article <Xns9717EE65C9BC1castleamber@130.133.1.4>, John Bokma
<john@castleamber.com> wrote:
> PerlFAQ Server <comdog@pair.com> wrote:
[color=darkred]
> Foo::amethod;
>
> is that a Foo object....
Don't call methods as subroutines :)
We could include absolutely everything a programmer needs to
think about, but that obscures the point of answer.
--
brian d foy, bdfoy@cpan.org
Subscribe to The Perl Review: http://www.theperlreview.com
| |
| John Bokma 2005-11-24, 6:57 pm |
| brian d foy <comdog@panix.com> wrote:
> In article <Xns9717EE65C9BC1castleamber@130.133.1.4>, John Bokma
> <john@castleamber.com> wrote:
>
>
>
>
>
> Don't call methods as subroutines :)
>
> We could include absolutely everything a programmer needs to
> think about, but that obscures the point of answer.
sub amethod {
my $self = shift;
my $class = ref($self) || $self;
if ( ref $self ) {
warn "called me from a $class object";
} else {
warn "called as a $class method";
}
}
I think a FAQ shouldn't confuse people. Just 4 extra lines are not going
to harm the answer (now lets hope this doesn't end in an endless
discussion)
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
| |
| brian d foy 2005-11-25, 6:59 pm |
| In article <Xns9718ADA569BF8castleamber@130.133.1.4>, John Bokma
<john@castleamber.com> wrote:
> brian d foy <comdog@panix.com> wrote:
>
[color=darkred]
[color=darkred]
> sub amethod {
>
> my $self = shift;
> my $class = ref($self) || $self;
> if ( ref $self ) {
>
> warn "called me from a $class object";
>
> } else {
>
> warn "called as a $class method";
> }
> }
> I think a FAQ shouldn't confuse people. Just 4 extra lines are not going
> to harm the answer
Okay, I can see that, but that's a long way from your original
comment about calling the method with the full package specification
and no arguments. :)
--
brian d foy, bdfoy@cpan.org
Subscribe to The Perl Review: http://www.theperlreview.com
| |
| John Bokma 2005-11-25, 6:59 pm |
| brian d foy <comdog@panix.com> wrote:
> In article <Xns9718ADA569BF8castleamber@130.133.1.4>, John Bokma
> <john@castleamber.com> wrote:
>
>
>
>
>
> Okay, I can see that, but that's a long way from your original
> comment about calling the method with the full package specification
> and no arguments. :)
Yes, apologies, my example was a bit badly written, but it did show that
amethod can give a confusing warning :-)
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
|
|
|
|
|