CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Discussion(maybe?) How would you do this? Modules and reading a file/dir

 



Iconx
Novice

Feb 25, 2014, 8:51 AM

Post #1 of 3 (1681 views)
Discussion(maybe?) How would you do this? Modules and reading a file/dir Can't Post

[This is cross-posted on devshed and perl monks due to not getting much answers]

This is a similar question to my previous one about modules but a more overhead question. The example is somewhat simplified.

Say you have a bunch of scripts like request.pl, modify.pl, transfer.pl, pending.pl etc.....

they all contain multiple subs where it reads a directory or contents of a comma separated file and will check for a variety of conditions so it can go on to modify or transfer or view etc.

Right now every script and alot of subs in them re-read the file or files in question, so alot of scripts have something like this, using a foreach, sometimes a while...etc


Code
 foreach my $file (@files) { 
chomp $file;
my $filename = "$file";
(open(my $IF, '<', $filename));
my @entry = <$IF>;
$lastentry = $entry[-1];
close $IF;
chomp $lastentry;
my @record = split(':', $lastentry);
my $id = $record[0];
my $name = $record[1];
my $street = $record[2];
my $city = $record[3];
my $state = $record[4];
my $status = $record[5];
if (some condition like $status is active)(
do something.....
)

So I thought instead of all this re-reading, what if a module was called to just read the files once and do different things in different scripts. Read the files, split up the commas in to variables or something and have it in an array or hash....and then based on conditions do something

Example: in the modify script read the files and determine which are in status active and do stuff, and in move.pl determine if city is something specific and do something else with it but not have to re-read the file in each applicable sub and script. I'm sorta missing something because in all cases the file being read is in a while or for loop so what do I do? Just return the array from the module and split in each file or what? Does this make sense?

How would you do this?


---Iconx


Laurent_R
Veteran / Moderator

Feb 25, 2014, 10:47 AM

Post #2 of 3 (1669 views)
Re: [Iconx] Discussion(maybe?) How would you do this? Modules and reading a file/dir [In reply to] Can't Post

As I said on one of your cross-posts, your question is too general. I can think of at least half a dozen techniques that might save you a lot of typing and duplicated code (callbacks, objects, closures, function factories, dispatch tables, etc.) but I just don't know which to really suggest because you are not being specific enough. Please provide a simple example of some code that you don't like because you think that there should be a better way of doing it, we might be able to help further.


FishMonger
Veteran / Moderator

Feb 25, 2014, 11:36 AM

Post #3 of 3 (1665 views)
Re: [Iconx] Discussion(maybe?) How would you do this? Modules and reading a file/dir [In reply to] Can't Post

I think your starting point should be to take a step back and create a detailed flowchart of your app without taking into account any of the code you currently have written.

Then look at your code to see how well it fits into your planed flowchart. You may find that your current scripts will only need minor adjustments, or you might find that they won't meet your needs without major rewrites.

Based on your limited and vague description, I'm willing to guess that you'll need to redesign/rewrite a lot of your current code.


(This post was edited by FishMonger on Feb 25, 2014, 11:36 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives