From: monkeys paw on
if we have a package and want to access the package name
using a perl var how is it done? I want the error message
to say (literally): "Mypack Error: hello, i am an error"

I want the error method to prepend "Mypack Error: " to any
error return. However, i don't want to manually enter the Mypack
string, i want to access thru $self or some such thing. How would
i do that


package Mypack;
use strict;

sub new {
my ($class, %opts) = @_;
my $self = \%opts;
bless $self, $class; # quicker than the foreach [BP - 16 Sep 2009]
$self->{error} = 'hello, i am an error';
return $self;
}

sub error {
my ($self, %opts) = @_;
# I would like to do something *like*
# return $self->{package} . 'Error: ' . $self->{error}
return 'Mypack Error: ' . $self->{error};
}
From: Big and Blue on
On 01/28/10 00:45, monkeys paw wrote:
> if we have a package and want to access the package name
> using a perl var how is it done?

my $package_name = __PACKAGE__;

or just interpolate __PACKAGE__ into the string.

It's like __FILE__ and __LINENO__.


> return 'Mypack Error: ' . $self->{error};

return __PACKAGE__ . ' Error: ' . $self->{error};


--
Just because I've written it doesn't mean that
either you or I have to believe it.
From: Uri Guttman on
>>>>> "mp" == monkeys paw <user(a)example.net> writes:

mp> if we have a package and want to access the package name
mp> using a perl var how is it done? I want the error message
mp> to say (literally): "Mypack Error: hello, i am an error"

perldoc -f ref

perldoc Carp

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ben Morrow on

Quoth Big and Blue <No_4(a)dsl.pipex.com>:
> On 01/28/10 00:45, monkeys paw wrote:
> > if we have a package and want to access the package name
> > using a perl var how is it done?
>
> my $package_name = __PACKAGE__;
>
> or just interpolate __PACKAGE__ into the string.

The __FOO__ literals don't interpolate. CLASS will give you $CLASS,
which does. (Why it's $CLASS rather than $PACKAGE I don't know.)

Ben