Package name in Perl

I

iris.a.jackson

Hi,

I am trying to find out how to create a variable in Perl whose value is
the name of the package it has been defined in. I haven't been able to
find the answer by Googling around.

Specifically, I'd like to be able to write Perl code like this:

=====
package My::Cool::pkg;

our $name = $XXX;
=====

[obviously $XXX would be replaced by some funny-looking thing that I
still need to learn] and I'd like $name to be assigned 'My::Cool::pkg'
when the file containing this is loaded. I would like to have this
available before any objects of class 'My::Cool::pkg' are created.

Thank you!
 
R

Robert 'phaylon' Sedlacek

(e-mail address removed) wrote
I am trying to find out how to create a variable in Perl whose value is
the name of the package it has been defined in. I haven't been able to
find the answer by Googling around.

The answer's not well hidden in perldoc perlmod :)

gr.,
Robert
 
P

Paul Lalli

I am trying to find out how to create a variable in Perl whose value is
the name of the package it has been defined in. I haven't been able to
find the answer by Googling around.

Specifically, I'd like to be able to write Perl code like this:

=====
package My::Cool::pkg;

our $name = $XXX;
=====

[obviously $XXX would be replaced by some funny-looking thing that I
still need to learn] and I'd like $name to be assigned 'My::Cool::pkg'
when the file containing this is loaded. I would like to have this
available before any objects of class 'My::Cool::pkg' are created.

$ perl -e'
package FOO;
my $name = __PACKAGE__;
print "Package: $name\n";
'
Package: FOO

Paul Lalli
 
P

Paul Lalli

I am trying to find out how to create a variable in Perl whose value is
the name of the package it has been defined in. I haven't been able to
find the answer by Googling around.

In addition to my other response, I must point out to you that
"Googling around" should be your *last* resort. Looking in the
built-in Perl documentation should be your first:

$ perldoc -q package
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq7.pod
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";
}


Paul Lalli
 
I

iris.a.jackson

Thank you very much for your extremely helpful replies. In the future I
will also try to look in the those manpages before asking here. Thanks
again!
 
T

Tad McClellan

Paul Lalli said:
In addition to my other response, I must point out to you that
"Googling around" should be your *last* resort.


That is not correct.

Posting to Usenet should be the last resort.

Googling should happen earlier than last...

Looking in the
built-in Perl documentation should be your first:


.... but later than first. :)
 
P

Paul Lalli

Tad said:
That is not correct.

Posting to Usenet should be the last resort.

Googling should happen earlier than last...

Clarification - I meant "last resort of *looking* for information."
Posting to Usenet is *asking* for information. :)

Paul Lalli
 
B

brian d foy

Paul said:
Clarification - I meant "last resort of *looking* for information."
Posting to Usenet is *asking* for information. :)

Googling is often my first resort. The perl documentation is a huge
beast. Telling people to look in it isn't very helpful if they don't
know where things are. Google, however, does it very well:

site:perldoc.perl.org <search terms>
 
B

Bart Lateur

Paul said:
In addition to my other response, I must point out to you that
"Googling around" should be your *last* resort.

Nah... asking *here* should be your last resort. Or at least, come
later.
Looking in the
built-in Perl documentation should be your first:

Amen to that.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top