Dynamically call built-in functions

T

Tomek

Hello!

I am having I suppose basic problem, however couldn't Google it :(

What I want to do is to format variable depending on the need.

E.g.

my @formats = qw(uc uc_first lc lc_first);
my $str = 'My name is tom';
for my $fun_name (@formats) {
my $result = &$fun_name($str);
print $result."\n";
}

I would like it to print:
1. CAPITALIZED ALL
2. Capitalized first
3. lowercased all
4. lowercased first.

However, I am getting:
Undefined subroutine &Resources::Words::uc called at Resources/Words.pm
line 60.

Please send me to appropiate FAQ if it has been already disputed here...

Best regards, T. Kraus
 
J

John W. Krahn

Tomek said:
I am having I suppose basic problem, however couldn't Google it :(

What I want to do is to format variable depending on the need.

E.g.

my @formats = qw(uc uc_first lc lc_first);
my $str = 'My name is tom';
for my $fun_name (@formats) {
my $result = &$fun_name($str);
print $result."\n";
}

my @formats = (
sub { uc $_[0] },
sub { ucfirst $_[0] },
sub { lc $_[0] },
sub { lcfirst $_[0] },
);

my $str = 'My name is tom';

for my $fun_name ( @formats ) {
my $result = $fun_name->( $str );
print "$result\n";
}



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top