Click here to get back home

Perl Special Variable Containing the name of the method currently in?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl Special Variable Containing the name of the method currently in? Nigel 05-30-2008
Get Chitika Premium
Posted by Nigel on May 30, 2008, 2:06 pm
Please log in for more thread options

Hello,

Just out of curiosity.
Is there a way to get the name of the subroutine Perl is currently in
when it is running?
For example, if I have a subroutine declared with the name sub
helloWorld, is there any way from within that subroutine to
programmatically get 'helloWorld' without having to hardcode it?

Thanks!
- Nigel

Posted by nolo contendere on May 30, 2008, 2:10 pm
Please log in for more thread options
> Hello,
>
> Just out of curiosity.
> Is there a way to get the name of the subroutine Perl is currently in
> when it is running?
> For example, if I have a subroutine declared with the name sub
> helloWorld, is there any way from within that subroutine to
> programmatically get 'helloWorld' without having to hardcode it?

yup. caller().

http://perldoc.perl.org/functions/caller.html

HTH,


Posted by A. Sinan Unur on May 30, 2008, 2:19 pm
Please log in for more thread options
fd8c4b7e758a@z66g2000hsc.googlegroups.com:

> Just out of curiosity.
> Is there a way to get the name of the subroutine Perl is currently in
> when it is running?
> For example, if I have a subroutine declared with the name sub
> helloWorld, is there any way from within that subroutine to
> programmatically get 'helloWorld' without having to hardcode it?

#!/usr/bin/perl

use strict;
use warnings;

sub whatismyname { (caller 1)[3] }

sub iknowmyname {
my $name = whatismyname();

print "My name is '$name'\n";
}

iknowmyname();

__END__

Make sure to read perldoc -f caller

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Frank Seitz on June 2, 2008, 10:13 am
Please log in for more thread options
A. Sinan Unur wrote:
> fd8c4b7e758a@z66g2000hsc.googlegroups.com:
>>
>>Is there a way to get the name of the subroutine Perl is currently in
>>when it is running?
>>For example, if I have a subroutine declared with the name sub
>>helloWorld, is there any way from within that subroutine to
>>programmatically get 'helloWorld' without having to hardcode it?
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> sub whatismyname { (caller 1)[3] }
>
> sub iknowmyname {
> my $name = whatismyname();
>
> print "My name is '$name'\n";
> }
>
> iknowmyname();

It should be mentioned that there are cases where
the code does not work as expected:

*f = \&iknowmyname;

iknowmyname();
f();

__END__
My name is 'main::iknowmyname'
My name is 'main::iknowmyname'

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by szr on June 2, 2008, 3:42 pm
Please log in for more thread options
Frank Seitz wrote:
> A. Sinan Unur wrote:
>> fd8c4b7e758a@z66g2000hsc.googlegroups.com:
>>>
>>> Is there a way to get the name of the subroutine Perl is currently
>>> in when it is running?
>>> For example, if I have a subroutine declared with the name sub
>>> helloWorld, is there any way from within that subroutine to
>>> programmatically get 'helloWorld' without having to hardcode it?
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> sub whatismyname { (caller 1)[3] }
>>
>> sub iknowmyname {
>> my $name = whatismyname();
>>
>> print "My name is '$name'\n";
>> }
>>
>> iknowmyname();
>
> It should be mentioned that there are cases where
> the code does not work as expected:
>
> *f = \&iknowmyname;
>
> iknowmyname();
> f();
>
> __END__
> My name is 'main::iknowmyname'
> My name is 'main::iknowmyname'

This would be exactly what I would expect. 'main::iknowmyname' is the
conical name of the function, where as 'f' is basically just a pointer
(ok, alias) and not the true name of the function, where as
'main::iknowmyname' is the true name :-)

--
szr



Similar ThreadsPosted
"Can't locate object method "PV" via package "B::SPECIAL"" October 19, 2007, 6:53 pm
Special variable "@$" June 22, 2006, 1:41 am
Iterator special variable $_ January 7, 2007, 5:04 pm
How to use special variable in regex? November 26, 2008, 10:49 pm
Special variable to read out ||-match June 1, 2006, 5:06 am
Using one methods variable value in another method inside a module April 29, 2007, 9:04 am
Overriding a class method with an object method July 2, 2007, 4:33 pm
Problem with AJAX and Special Chars (Perl, MySQL 4) March 16, 2006, 11:40 am
perl threading; ->join; best method? November 9, 2006, 2:30 pm
new method to test Perl code February 21, 2007, 5:02 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap