Andreas Jacobsen's Distraction

Another cause of procrastination

Finding the name of a Perl sub routine from its reference

leave a comment »

While mucking around with Perl’s Test::Simple I needed to find out the name of a sub routine from a reference. While there are several answers, finding any useful ones by the usual means was difficult. The one I landed on was using Sub::Identify, which provides the routine sub_name($) that solves the problem nicely.

Here’s some example code:

perl -MSub::Identify -e 'sub bar{return "baz"};$foo=\&bar;print Sub::Identify::sub_name($foo). "\n"'

This correctly prints “bar”.

Advertisement

Written by Andreas

November 17, 2009 at 12:54

Posted in Tips & Tricks

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.