Is there a way to fully qualify a subroutine with the package name in a variable but without using eval?

This is possible with OO programming, where a long class name can be replaced with a variable:

my $class = 'Very::Long::Class::Name'; $class->method;

I want to do a similar thing for non-OO subroutines, so I can add clarity to the program by fully qualifying the sub name but still keep the call to a manageable length (and without sprinkling eval everywhere). Doing this:

my $pkg = 'Very::Long::Package::Name'; $pkg::sub;
obviously doesn't work, since $pkg::sub refers to the global variable $sub in the pkg namespace.

I've considered the following options (example code below):

use strict; use warnings; { package very::long::packagename; sub foo { print '[', join( '][', @_ ), "]\n"; return "from sub foo\n"; } } { package pkg; our $foo = "foovar\n"; } { package bar; # this refers to $foo in package 'pkg' print $pkg::foo; # foovar # fully qualified sub call very::long::packagename::foo( 'param' ); # [param] # create a ref to a fully qualified sub my $subfoo_ref = \&very::long::packagename::foo; print $subfoo_ref->( 'param' ); # from sub foo $subfoo_ref->( 'param' ); # [param] # call the sub as a method my $pkg = 'very::long::packagename'; $pkg->foo( 'param' ); # [very::long::packagename][param] # stringify the call, use eval my $func_str = $pkg . "::foo( 'param' );"; eval $func_str; # [param] }

Are there other ways of doing this that are not listed above? Comments regarding best practices are also appreciated.


In reply to Alternative ways to fully qualify subroutine names by bobf

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":