• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

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

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?
-----------------------
$dir="/var/www/vhosts/domain/Data";
use utf8;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use::strict;

$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);
close(DIR);
open(DIRA, ">>$dir/color.dat") || die "Can't open $dir/color.dat";
flock (DIRA, 2);
$color = <DIRA>;
flock (DIRA, 8);
close(DIRA);
open(DIRB, ">>$dir/color.dat") || die "Can't open $dir/name.dat";
flock (DIRB, 2);
$color = <DIRB>;
flock (DIRB, 8);
close(DIRB);
-----------------------
0
ikon32
Asked:
ikon32
1 Solution
 
ikon32Author Commented:
Typo, the open line is:
open(DIR, ">>$dir/$id/name.dat
0
 
ozoCommented:
my $id = $q->param('id');
open(DIR, ">>$dir/$id/name.dat
#this is not safe a client could trick you into opening any file
0
 
ikon32Author Commented:
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.
0
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.

 
FishMongerCommented:
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 $!.

0
 
whosbetterthanmeCommented:
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?
0
 
ikon32Author Commented:
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
----------------------------------------
second,
(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...


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now