Dynamically call built-in functions

Discussion in 'Perl Misc' started by Tomek, Apr 7, 2007.

  1. Tomek

    Tomek Guest

    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
     
    Tomek, Apr 7, 2007
    #1
    1. Advertising

  2. Tomek wrote:
    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Apr 8, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,611
    Victor Bazarov
    Jun 21, 2004
  2. Allen
    Replies:
    9
    Views:
    1,051
    Thomas J. Gritzan
    Jun 16, 2007
  3. beginner
    Replies:
    7
    Views:
    554
    Alex Martelli
    Aug 19, 2007
  4. PokerJoker

    Accessing data from Dynamically built Datagrid

    PokerJoker, Aug 27, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    125
    PokerJoker
    Aug 27, 2004
  5. RCorona

    ItemCommand on dynamically built DataGrid

    RCorona, Dec 21, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    111
    RCorona
    Dec 21, 2004
Loading...

Share This Page