Monks,

Suppose one a subroutine and a scalar that contains the name of the subroutine. How does one call the subroutine based on the content of the scalar?

use strict; use warnings; sub foo { print 'foo\n'; } sub bar { print 'bar\n'; } my $var = 'foo'; # invoke sub foo __END__

Update Tue Apr 15 07:32:23 CEST 2008: What I am actually trying to do is this:

I intend to create a script that takes two files as arguments: A (CSV) data file, and a (Config::IniFiles) configuration file. The data file will look like this:

# col1;col2;col3; 123456;hey hey;2008-04-14 123457;my my;2008-04-15
The configuration file will look like this:
col1=is_int col2=is_string col3=is_yyyy_mm_dd_date
Each key in the configuration file refers to a column in the data file. Each value corresponds to a subroutine that returns true or false depending on whether the data passes validation or not. The list of validation functions grows quickly, and I was hoping to avoid having to maintain an ever-growing list of if-else expressions or a giant hash that maps each string expression to the given subroutine. I am aware of the security risks, although I believe there can be measures put in place to ensure that only pre-defined subroutine can be called.

Any tips or hints on how to otherwise solve this problem is welcome.

--
Andreas

In reply to Invoke sub whose name is value of scalar by andreas1234567

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.