I would like to call a sub where the sub name is the same as the changing file name (as I read each file in the directory). For example, I would like to call sub "allergies" if I am reading the allergies file. I would like to call sub "immunizations" if I am reading the immunizations file. I could add a lot of if statements but there must be a clean way to to accomplist this task. Does anyone have an idea?

The line in the code below is "$file();"

Thanks, Kevin
# version 1.0 use warnings; use strict; use Shell; my $dir = 'input_files'; die "can't opendir $!" unless opendir DIR, $dir; while (defined(my $file = readdir DIR)) { do { print "The directory and file are $dir/$file\n"; die "Can't open input file $!" unless open IN, "< $dir/$file"; use File::Glob (); while (defined($_ = glob(' IN '))) { # print "Hello $_"; } while (<IN>){ # Reading contents of file next if (/^#/); $file(); ##### File name can be many things print "Bye " . $_ ; } }; } closedir DIR; sub allergies { print "in allergies\n"; } sub immunizations { print "in immunizations\n"; }

In reply to How to call a Sub / Function with changing name by kevyt

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.