Prev: print array with separator?
Next: FAQ 4.1 Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
From: monkeys paw on 27 Jan 2010 19:45 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 27 Jan 2010 19:50 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 27 Jan 2010 19:55 >>>>> "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 27 Jan 2010 20:22
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 |