mildewhall has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, I am in the process of converting about 150 individual data conversion scripts to a single, common script. I have concluded that 99% of the code is common to all users, and just 1% is user-specific. What I want to be able to do is to pass the user-name as a parameter, like "convert client1" and have a sub in the script hook out the relevant block of conversion code. Like so..
common code; include $ARGV[0].code; more common code;
Here's an example of my user-specific code:
if ( m/(\S+),(\S+),(\S+),(\S+),(\S+),(\S+)/o ) { @fields = split(/,/); $CLI = $fields[0]; $key = $fields[1]; $product = $fields[2]; $quantity = $fields[3]; $price = $fields[4]; $salesdate = $fields[5]; }
Problem is, I am not aware of an "include" facility in Perl (called include), and can't think of an obvious workaround. The problem as I see it is that the code segment needs to be pulled in and executed as patr of the main script: will this work ? TIA

Replies are listed 'Best First'.
Re: How can i "include" an executable section of code
by Zaxo (Archbishop) on Jan 13, 2004 at 09:34 UTC

    Something like this: my $hashref = do $path . $clientfile;

    After Compline,
    Zaxo

      Wow ! Can't believe it was that simple.. next time you're in Alton I'll buy you a beer :-) Thanks to all those that offered suggestions.
Re: How can i "include" an executable section of code
by davido (Cardinal) on Jan 13, 2004 at 09:36 UTC
    Yes, Perl doesn't have include. It doesn't have a preprocessor like C does either.

    However, it does have use, require, do, and eval.


    Dave

      Of course, you can use the C pre-processor if that floats your boat. It's probably not the right solution though :-)
      As a followup, having tested the "do" option, I found that using "require" was altogether the best option, especially in terms of memory usage on large files.
      $client = $Options{'c'}; # # call in the relevant client code # require "$client.segment"; #
      and the $client.segment file contains
      sub DoClient { # client 'A' my $EURO = 1.35; if ( m/(\S+),(\S+),(\S+),(\S+),(\S+),(\S+)/o ) { @fields = split(/,/); ...... } }1;