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

hey...i dont know what is the problem in this script...but it says cant open the file....
#!/usr/bin/perl -w use strict; my ($dir,$file,$old,$new); print "Enter Directory path: "; $dir = <STDIN>; print "Enter filename: "; $file = <STDIN>; print "Enter old pattern: "; $old = <STDIN>; print "Enter new pattern: "; $new = <STDIN>; chomp ($dir,$file,$old,$new); opendir (DIR, "$dir") || die $!; my @files = readdir(DIR); foreach my $filename( @files) { if ("$file" =~ "$filename") { open (FH, "$file") || die "cant open file $!"; my @lines = <FH>; print @lines; foreach (@lines) { if (grep /"$old"/, $_) { s/"$old"/$new"/g; print $_; }} }}
it says/..............
cant open file No such file or directory at ./change.pl line 18,

Replies are listed 'Best First'.
Re: reading file in directory
by GrandFather (Saint) on Jan 27, 2011 at 07:15 UTC

    Maybe the file doesn't exist or you haven't appropriate permissions? Or maybe the file is not where you think it is? Note that readdir doesn't prepend the directory path used in opendir to the values returned so you have to do that yourself.

    It helps to include the file name that was used in the open in the error message so you can verify that the file you were attempting to open was the file you intended to open.

    True laziness is hard work
Re: reading file in directory
by Ratazong (Monsignor) on Jan 27, 2011 at 07:32 UTC

    It would be nice if you could have formatted your text with code-tags ... then it would be easier to identify line 18.

    Nevertheless there are some issues in your code:

    if ("$file" =~ "$filename")
    What do you want to acchieve with this code? It makes much more sense to use eq insted of =~ ... or change the order ("is the entered filename part of the real filename?")
    open (FH, "$file") || die "cant open file $!";
    It would have helped you to display also the filename you couldn't open. Then you would have seen that it does not contain the path. Assuming you want to open the file with the real filename (and not the filename the user entered), your could change your code to
    $filename = $dir."/".$filename; open (FH, "$filename") || die "cant open file <$filename> $!";
    (note: the separator between directory and filename might be different in your operating system)

    Now you can attack the issue with your search&replace ... there is an odd number of quotation marks (") inside your substitution, which should make you very suspicious ;-) And think again why you use grep AND s///.

    HTH, Rata
Re: reading file in directory
by kansal611 (Initiate) on Jan 27, 2011 at 09:27 UTC
    Thanx for the replies.......
    Solved the problem by using current directory......
    #!/usr/bin/perl -w my ($dir,$file,$old,$new); print "Enter filename: "; $file = <STDIN>; print "Enter the name of output file: "; $out = <STDIN>; print "Enter old pattern: "; $old = <STDIN>; print "Enter new pattern: "; $new = <STDIN>; chomp ($file,$old,$new, $out); open (OUT, ">>$out") || die $!; opendir (DIR, ".") || die $!; my @files = readdir(DIR); open (FH, "$file") || die "cant open file $!"; my @lines = <FH>; foreach my $line (@lines) { if (grep /$old/,$line) {$line =~ s/$old\s/$new/g;} print OUT $line; }