goldcougar has asked for the wisdom of the Perl Monks concerning the following question:
Hello. I've coded in perl in the past, but its been a bit since I've coded regularly in perl. I've recently started working with a consultant that decide to use perl for a project. I took a look at his code, and I was immediately surprised by what I found. Given that I am not a professional perl programmer, I wanted to check with a group of professionals to confirm my suspicions, and see if this is being done the wrong way.
The overall task of the code is to take files submitted by a customer which is in various formats, and convert them into a format to be imported into a database (PostgreSQL to be specific). The code is executed via Windows command line. I can't post entire code, since its the customers product, but I will post some snippets here to protect the innocent.
Reusing code- The code consists of 5 different perl files all in the same system directory. For sub functions, he uses code like below. However, there is no reuse of the subs across the files. So, for example sub doXYZ exists in all 5 of the perl files, duplicated exactly. Shouldn't he be creating a module for the shared sub functions and including it, so the code for these is shared?
sub doXYZ{ ... }
File IO- The code involves some moving, renaming, copying, deleting etc of files and folders. Perl has great built-in File IO libraries from my experience. However, he builds a string of the windows command he wants to run, and runs it via System. I have never seen anyone do this before. Am I crazy, or is this the wrong way?
#Delete Example $theCmd="del "."\"".$SourcePath."\" >> ".$LOG_DIR."\\".$F_DUMMY." 2> +&1"; $returnCode=system $theCmd; #Delete Example $theCmd="copy "."\"".$SourcePath."\\".$SourcePathFileName."\""." "."\" +".$DestPath."\\".$DestPahtFileName."\""." >> ".$LOG_DIR."\\".$F_DUMMY +." 2>&1"; $returnCode=system $theCmd;
Monitoring for new files- The code needs to check when a new file is added, and then start processing it. The code uses an infinite loop to monitor for files. Shouldn't he be using one of the Scheduling CRON modules in Perl, or scheduling it via the windows system task scheduler? I pointed this one out already, and the developer said this was just the easy way to do it and he wasn't sure how the customer wanted to schedule it. However, I can't see why anyone would ever do it this way, and not just do it the right way the first time?
while(1==1){ #code here for processing files sleep(60); }
Connecting to SQL Database- The code needs to have the output file it builds to be imported into the PostgreSQL database. It does this by writing to a hard coded file with a list of file names, return delimited, that is has finished. Then there is a PostgreSQL scheduled job looking for that file, importing it into a table. It then has the filenames, and then goes out again and issues the PostgreSQL COPY command to copy that file into the appropriate final table. My experience here is that passing parameters to another system (from Perl to PostgreSQL) should never be done by writing to a shared file that the other program reads. This always causes problems with concurrency, and it just bad practice. In addition, Perl has many connectors to databases available, like to PostgreSQL, and it can make its own connection to PostgreSQL and issue the COPY command directly. I've used DBI in the past to connect to other SQL databases and it was simple and easy.
#FilesToPostgres.txt looks like this: processedFileName1.csv processedFielName1.csv #PostgreSQL then has a scheduler running which reads that file into a +table, and then loops its rows and runs subsequent commands like COPY tableName FROM /path/to/processedFileName1.csv
So, I was hoping to get some experts to let me know if what this programmer is doing is acceptable, or if I'm correct that we have some issues here that we need to fix, and this programmer doesn't seem to understand the basics of perl (which by the way was his language of choice, as he could have used whatever he wanted). I hate seeing new projects start with bad code, so I want to get it right from the start, but this guy considers himself a senior developer and I don't want to offend him, unless I know for sure there are some issues
Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Advice on best practices
by davido (Cardinal) on Mar 06, 2015 at 03:58 UTC | |
by stonecolddevin (Parson) on Mar 06, 2015 at 17:45 UTC | |
|
Re: Advice on best practices
by GrandFather (Saint) on Mar 05, 2015 at 20:12 UTC | |
|
Re: Advice on best practices
by Anonymous Monk on Mar 05, 2015 at 21:00 UTC | |
|
Re: Advice on best practices
by GotToBTru (Prior) on Mar 05, 2015 at 19:38 UTC | |
|
Re: Advice on best practices
by karlgoethebier (Abbot) on Mar 05, 2015 at 18:04 UTC | |
by goldcougar (Novice) on Mar 05, 2015 at 19:18 UTC | |
by karlgoethebier (Abbot) on Mar 05, 2015 at 20:03 UTC | |
|
Re: Advice on best practices
by goldcougar (Novice) on Mar 05, 2015 at 23:54 UTC | |
by Anonymous Monk on Mar 06, 2015 at 01:07 UTC | |
|
Re: Advice on best practices
by Anonymous Monk on Mar 06, 2015 at 02:28 UTC | |
|
Re: Advice on best practices
by stonecolddevin (Parson) on Mar 06, 2015 at 17:34 UTC | |
|
Re: Advice on best practices
by clueless newbie (Curate) on Mar 05, 2015 at 20:46 UTC | |
|
Re: Advice on best practices
by locked_user sundialsvc4 (Abbot) on Mar 05, 2015 at 22:51 UTC | |
by Anonymous Monk on Mar 05, 2015 at 23:18 UTC |