|
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
|