Is it possible in Perl to require a subroutine call is made?

Posted by MitchelWB on Stack Overflow See other posts from Stack Overflow or by MitchelWB
Published on 2012-09-10T20:06:50Z Indexed on 2012/09/10 21:38 UTC
Read the original article Hit count: 237

I don't know enough about Perl to even know what I'm asking for exactly, but I'm writing a series of subroutines to be available for many individual scripts that all process different incoming flat files. The process is far from perfect, but it's what I've got to deal with and I'm trying to build myself a small library of subs that make it easier for me to manage it all. Each script handles a different incoming flat file with it's own formatting, sorting, grouping and outputting requirements. One common aspect is that we have small text files that house counters that are used to name the output files so that we have no duplicate file names.

Because the processing of the data is different for each file, I need to open the file to get my counter value, because this is a common operation, I'd like to put it in a sub to retrieve the counter. But then need to write specific code to process the data. And would like a second sub that allows me to update the counter with the counter once I've processed the data.

Is there a way to make the second sub call a requirement if the first one is called? Ideally if it could even be an error that would prevent the script from running at all much like a syntax error.

EDIT: Here is a little [ugly and simplified] psuedo-code to give a better feel for what the current process is:

require "importLibrary.plx";

#open data source file
open DataIn, $filename;

#call getCounterInfo from importLibrary.plx to get the counter value from counter file
$counter = &getCounterInfo($counterFileName);

while (<DataIn>) {
  #Process data based on unique formatting and requirements
  #output to task files based on requirements and name files using the $counter
  #increment $counter
}

#update counter file with new value of $counter
&updateCounterInfo($counter);

© Stack Overflow or respective owner

Related posts about perl

Related posts about compiler-errors