What is the current package?

  • Thread starter Hallvard B Furuseth
  • Start date
H

Hallvard B Furuseth

Is there a standard way to refer to the current package? It feels a
little strange for code in a class to put its own package name in
quotes. Though I suppose I could do this:

package Foo;
use constant This_class => ref(bless []); # "Foo"
...
if ($val->isa(This_class)) { ... }
 
P

Paul Lalli

Is there a standard way to refer to the current package? It feels a
little strange for code in a class to put its own package name in
quotes. Though I suppose I could do this:

package Foo;
use constant This_class => ref(bless []); # "Foo"
...
if ($val->isa(This_class)) { ... }

perldoc -q 'current package'


Sheesh.
 
B

Brad Baxter

Is there a standard way to refer to the current package? It feels a
little strange for code in a class to put its own package name in
quotes. Though I suppose I could do this:

package Foo;
use constant This_class => ref(bless []); # "Foo"
...
if ($val->isa(This_class)) { ... }

Is this what you're after:

package Foo;
use constant This_class => __PACKAGE__;
my $val = bless [], __PACKAGE__;
if($val->isa(This_class)) { print "Yes!\n" }
print ref($val), "\n";

__END__

Yes!
Foo

Regards,

Brad
 
H

Hallvard B Furuseth

Paul said:
perldoc -q 'current package'

Sheesh.

Not in per5.004_04. Perhaps it's time to ask someone to upgrade...
Anyway, I found a newer Perl where that command worked. Thanks.
 
H

Hallvard B Furuseth

Brad said:
Is this what you're after:

package Foo;
use constant This_class => __PACKAGE__;
my $val = bless [], __PACKAGE__;
if($val->isa(This_class)) { print "Yes!\n" }
print ref($val), "\n";

Yup. Except I hoped to avoid This_class.
$val->isa(__PACKAGE__) looks nicer.
Thanks.
 
P

Paul Lalli

Not in per5.004_04. Perhaps it's time to ask someone to upgrade...
Anyway, I found a newer Perl where that command worked. Thanks.

I don't want to be too much of a bastard, but having an outdated version
of perl is not an excuse for not reading the FAQs.

www.perl.com -> documentation -> FAQs -> About the Perl FAQ
search for "current package".

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top