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

I have this code, where I want to handle multiple csv(currently its just one file) file and use perl to render its format before its shipped to a linux-box and replaces original file content using ssh connection. Here is the code
#!/usr/bin/perl -w use strict; my $seculert_qradar_list = "$seculert_dir/seculert.csv"; my $qradar_console = '10.10.1.22'; my $qradar_ssh_key = "$seculert_dir/qr-id_dsa"; my $qradar_ssh_knownhosts = "$seculert_dir/known_hosts"; my $source = 'BAD-IP-Addresses-LABEL'; my $type_description = 'honeypots-for-examnple'; open(FP, ">>$seculert_qradar_list"); for my $line (<FP>) { my ($hostname, $ip, $something1, $something2) = split(/,/, $li +ne); print OUT "$source $type_description $ip #FF0000 0 90 29\n"; } close(FP); print "Sending to QRadar...\n"; # SSH To QRadar's Console and push out file + trigger update `scp -i $qradar_ssh_key -o UserKnownHostsFile=$qradar_ssh_knownhos +ts -o StrictHostKeyChecking=no root\@$qradar_console:/store/configser +vices/staging/globalconfig/remotenet.conf .`; `sed -i -e '/^SECULERT/d' remotenet.conf`; `cat $seculert_qradar_list >> remotenet.conf`; `scp -i $qradar_ssh_key -o UserKnownHostsFile=$qradar_ssh_knownhos +ts -o StrictHostKeyChecking=no remotenet.conf root\@$qradar_console:/ +store/configservices/staging/globalconfig/remotenet.conf`; print "Cleaning up...\n"; # Remove our SECULERT list and the newly pushed out qradar conf unlink($seculert_qradar_list); unlink ('remotenet.conf'); print "Deploying in QRadar...(takes time to complete)\n"; # QRadar magic `ssh -i $qradar_ssh_key -o UserKnownHostsFile=$qradar_ssh_knownhos +ts -o StrictHostKeyChecking=no root\@$qradar_console /opt/qradar/upgr +ade/util/setup/upgrades/do_deploy.pl`; print "Complete!\n\n";

What I'm interested to know and perhaps get some help from perl programmer that using one file handle can I open multiple files e.g In my case I have something like this 1. virus.csv 2. bot.csv 3. malware.csv

Do i need to re-copy for loop code for each csv file with a different handle? The destination file remotenet.conf remains the same.

After correct rendering of for e.g one csv file the remotenet.conf on web-ui would look something like Virus 10.10.2.1 ....... Bot 10.10.4.1 ...... It would be great that multiple changes happen in one-go, with just one auto-deploy(see code at end). I hope I'm able to understand the problem. Please let me know if more clarification is required. thanks **UPDATE** Based upon content of csv rendering would change $source and $type_description varies with csv. for e.g malware.csv would have $source ='malware' and $type_description='top 10' and etc

Replies are listed 'Best First'.
Re: Multiple csv file rendering by PERL using single file open handle?
by CountZero (Bishop) on Dec 21, 2013 at 12:09 UTC
    You can (re-)use the same filehandle but you will have to close and re-open it for each next file

    That being said, your code has some problems (I will mention a few, there may be more):

    1. $seculert_dir is not declared nor defined. That should have given you an error message.
    2. The OUT filehandle is not opened.
    3. open(FP, ">>$seculert_qradar_list") opens a filehandle for appending, but you are reading from it.
    .

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Multiple csv file rendering by PERL using single file open handle?
by Laurent_R (Canon) on Dec 21, 2013 at 12:01 UTC
    If I understand correctly your question, yes, you can use the same filehandler successively for processing different files, provided that you open each file, process it and close it successively. This is an example Perl one-liner where I use the same $IN handler to process (in this case just print to the screen) successively two files:
    $ perl -e 'for my $file (qw /pattern.pl line_count.pl /) {open my $IN, + "<", $file or die $!; print while <$IN>; close $IN;}'
    This prints to my screen the content of the "pattern.pl" and "line_count.pl" files. (There are easier ways to do just that, e.g. using the "p" command line option, but the aim here is just to demonstrate the syntax. An additional remark is that it is not even necessary to close the files in the example above, because the files would be closed automatically when the lexical filehandler goes out of scope at the end of the loop iteration, but I consider it is better to do it explicitly.)