open directory and read multiple files at the same time in perl

Posted on 2010-01-12
Medium Priority
Last Modified: 2012-06-27
Hi, I need to open a directory and read afew existing files from it. I can use something like this but it must be a better and faster way to do it. Can anyone help me with it, please?
use utf8;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

$q = new CGI;
my $id = $q->param('id');
open(DIR, ">>$dir/name.dat") || die "Can't open $dir/name.dat";
flock (DIR, 2);
$name = <DIR>;
flock (DIR, 8);
open(DIRA, ">>$dir/color.dat") || die "Can't open $dir/color.dat";
flock (DIRA, 2);
$color = <DIRA>;
flock (DIRA, 8);
open(DIRB, ">>$dir/color.dat") || die "Can't open $dir/name.dat";
flock (DIRB, 2);
$color = <DIRB>;
flock (DIRB, 8);
Question by:ikon32
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 26291628
Typo, the open line is:
open(DIR, ">>$dir/$id/name.dat
LVL 84

Expert Comment

ID: 26291695
my $id = $q->param('id');
open(DIR, ">>$dir/$id/name.dat
#this is not safe a client could trick you into opening any file

Author Comment

ID: 26291737
The $id is not anything of great interest is not a pass or username... it just help to collect some data for the product at this example... Any ideas? and by the way how can that be done?
(#this is not safe a client could trick you into opening any file) Thanks.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 28

Accepted Solution

FishMonger earned 2000 total points
ID: 26292895
Is that the actual code you tested or did you retype it when you posted your question?

>> use::strict;
That line is not doing what you think and will generate this:
Useless use of a constant in void context

After fixing that, the code won't even compile.

What portion of that code do you feel is too slow and how did you determine that?

You're opening each of the files in write/append mode, but then attempt to read-in the contents.  That's not going to work.

It's better to use a lexical var for the filehandle instead of the bareword and it would also be better to use the 3 arg form of open.  The die statement should include the reason it failed which is in $!.


Expert Comment

ID: 26295120
I'm not sure what you mean. Are you trying to open the .dat files, then read lines from the files? Or do you want to open a directory and read the contents of the directory?

Author Comment

ID: 26295774
FishMonger, thanks I corrected most of it still have 2 questions for you,
why ozo thinks this is not safe and how the client can use this to open any file in the domain?
my $id = $q->param('id');
open(DIR, ">>$dir/$id/name.dat
#this is not safe a client could trick you into opening any file
(What portion of that code do you feel is too slow and how did you determine that?)
if this code is used to do this for lets say 50 of these files then maybe there is a solution that uses less memory or be a few microseconds faster.

what do you think...


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question