Vinay Verma
Vinay Verma

Reputation: 593

Issue while opening the file in Perl

I am trying to open a file using perl command. When I am passing a variable in between the file path, perl is not able to open the file: (here $line)

open(FL, "<C:/Users/admin/Documents/Projects/$line/project.txt") or die "Could not open file $!";

and If I try to open the file by passing the full path instead of variable ($line) in the path, Perl is able to open the file successfully:

open(FL, "<C:/Users/admin/Documents/Projects/projectName/project.txt") or die "Could not open file $!";

Could somebody please guide me how to achieve this. I have a list of projects and I cannnot pass name of every project manually but I need to achieve this by passing projectNames as the variables $line.

NOTE: All the $line variables are my projectNames.

Upvotes: 0

Views: 147

Answers (2)

mkHun
mkHun

Reputation: 5927

Suppose your $line having some special character it won't allow, so we shopuld escape the character, use quotemeta

$line = qutoemeta($line); 
open  my $fh, "<" ,$line or die "Error opening $!";

You will fix the error by $!. Already you have added it in your code. Please follow what it is saying.

Upvotes: 1

Błotosmętek
Błotosmętek

Reputation: 12927

Are you reading your project names from another file? If so, did you remember to remove \n at the end (using chomp $line or similar)?

Upvotes: 4

Related Questions