Issue while opening the file in Perl - 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.

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)?

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.

Related

In Perl, why can't I open this file using single argument open?

The file is 52MB in size. It is in the same directory as the program.
$big = 'VIXhx.csv';
# tie #optLine, 'Tie::File', $big or die "Cant Tie to $big $!" ;
open $big or die "Cant open $big, $!," ;
Tie::File gave no error message.
Plain open gave the error message:
Cant open VIXhx.csv, No such file or directory, at C:\Python34\hsf\ETFs\VIX\qad.
pl line 47.
(Yes, it's in the Python directory - but Perl works fine there)
I can open the file in the editor, so there doesn't seem to be a problem with the file itself.
I have a smaller file in the same program that opened with no problem in Tie::File.
$dat = 'ETF5Y.csv';
tie #datLine, 'Tie::File', $dat or die "Cant Tie to $dat $!" ;
Is it possible that Perl is unable to open a file if it's too large?
Please check perldoc -f open on how to open files, what you did ended up in opening an empty filename,
strace perl -e '$big = "/etc/passwd"; open $big or die "Cant open $big, $!,"'
output
...
open("", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "Cant open /etc/passwd, No such f"..., 64Cant open /etc/passwd, No such file or directory, at -e line 1.
See perldoc perlopentut:
Single Argument Open
Remember how we said that Perl's open took two arguments? That was a passive prevarication. You see, it can also take just one argument. If and only if the variable is a global variable, not a lexical, you can pass open just one argument, the filehandle, and it will get the path from the global scalar variable of the same name.
$FILE = "/etc/motd";
open FILE or die "can't open $FILE: $!";
while (<FILE>) {
# whatever
}
Therefore, if you want single argument open to do what you want, you would have to write our code as
$big = 'VIXhx.csv';
open big or die "Can't open '$big': $!";
# ^ <-- look, no dollar sign before filehandle
Alternatively, you could do something like this:
$big = 'VIXhx.csv';
*{$big} = \$big;
open $big and print <$big>;
if you want to keep the open $big.
But, relying on global variables and effects at a distance is not a good idea. Instead, use the three argument form of open to specify the filehandle, the mode, and the file name separately as in:
open my $vix_fh, '<', $vix_file
or die "Failed to open '$vix_file': $!";
By the way, you won't even find this section on "Single Argument Open" in recent Perl documentation. The following note should give you and idea why:
Why is this here? Someone has to cater to the hysterical porpoises. It's something that's been in Perl since the very beginning, if not before.
The single argument open can also be used to turn any Perl program into a quine.
I found the answer to my original question, of why TIE didn't work.
It turns out that the file used '0A' as the line terminator, so TIE, expecting '0D0A', read the whole 52MB file as one record.
I added recsep => "\n" to the TIE statement, and everything works fine.

Can't open file error in Perl

Why program can't open the file? i.e dies. I searched for this problem, but it seems all fine to me.
Funny thing is this code worked before, and i don't think i changed something from that moment in open function.
my $i;
my $regex = $ARGV[0];
for (#ARGV[1 .. $#ARGV]){
open (my $fh, "<", "$_") or die ("Can't open, $!");
$i++;
foreach (<$fh>){
print "Given regexp: $regex\nfile$i:\n line $.: $1\n" if $_ =~ /(\b$regex\b)/;
}
}
OUTPUT:
Can't open Not a directory
Not a directory means you're supplying an argument that assumes a non-directory is a directory.
For instance, if your argument is
a/b
and
a
exists but is not a directory, you will get this error.
Check your argument. it should be a proper directory name

Perl Readline on closed filehandle - file does not exist error

I am working on a simple perl program for my first assignment in my programming class. I literally have been stuck on this first part for more than a day. I cannot get my program to simply open a text file that is in the same directory as the program.
#!/usr/bin/perl -w
use strict;
my($fileName, $line);
print "Please enter name of file to be opened: ", "\n";
$fileName = <STDIN>;
chop($fileName);
#Associates FILE filehandle with the file: "filename.txt"
open(FILE, $fileName) or die("Can't open '$fileName': $!");
while(my $line = <FILE>){
print $line;
}
I am using strawberry perl. To run the program I am dragging and dropping the program into the command line to get the address of the program. It then attempts to run it.
It initially gave me a readline on closed filehandle error, and then I included the or die("Can't open '$fileName': $!"); portion of the code.
Now it says that there is no such file at the directory, but I know that the test.txt file is there because I just created it.
Picture of the code results: http://imgur.com/R8s7FFE
File directory that shows locations of my files: http://imgur.com/nUfM4lA)
The prompt is showing C:\Users\jacjar\Documents as the current working directory
So this is where the program will look for test.txt
But it is not in that directory
text.txt is in L:\College\Junior Year\1st Semester\COSC 320 (Programming Languages)
Move your test.txt file to the C: path shown above and it will work
Do you realize you are trying to open C:\User\jacjar\Documents\test.txt?

Can directory names and file names be variables in cgi perl scripting

I know that file names can be variable in perl scripting.Does the same apply to cgi-perl scripting.Because when I used variables inside open statement I get the error No such file or directory.But when I directly mention the path the file is opened for reading.These variables are passed from a form. The values are passed correctly they are not empty(Checked by printing the varaiables).
Example:
$dir=abc;
$file=file1;
open (FILE, '/var/www/cgi-bin/$dir/$file')
or print "file cannot be opened $!\n";
Error:
file cannot be opened no such file or directory.
Use double quotes to interpolate variables:
open (FILE, "/var/www/cgi-bin/$dir/$file")
# here __^ and here __^
or print "file cannot be opened $!\n";
Also, ALWAYS
use strict;
use warnings;
By using single quotes, the varibles aren'tt interpolated, so you're trying to open literally /var/www/cgi-bin/$dir/$file and it doesn't exist.
You've got (and accepted) a good answer. I just wanted to add that you can make your error message a lot more helpful if you include the value of $file in the string.
my $file_path = '/var/www/cgi-bin/$dir/$file';
open (FILE, $file_path)
or print "file [$file_path] cannot be opened: $!\n";
Then the error would have been "file [/var/www/cgi-bin/$dir/$file] cannot be opened: no such file or directory" which would have made it obvious that the variables weren't being expanded.
Update: I was talking nonsense. The new version is better.

Perl : Cannot open a file with filename passed as argument

I am passing two filenames from a DOS batch file to a Perl script.
my $InputFileName = $ARGV[0];
my $OutputFileName = $ARGV[1];
Only the input file physically exists while the Outputfile must be created by the script.
open HANDLE, $OutputFileName or die $!;
open (HANDLE, ">$OutputFileName);
open HANDLE, ">$OutputFileName" or die $!;
All three fail.
However the following works fine.
open HANDLE, ">FileName.Txt" or die $!;
What is the correct syntax?
Edit : Error message is : No such file or directory at Batchfile.pl at line nn
The proper way is to use the three-parameter form of open (with the mode as a separate parameter) with lexical file handles. Also die doesn't have a capital D.
Like this
open my $out, '>', $OutputFileName or die $!;
but your last example should work assuming you have spelled die properly in your actual code.
If you are providing a path to the filename that doesn't exist then you also need to create the intermediate directories.
The die string will tell you the exact problem. What message do you get when this fails?
code:
$file_name = $ARGV[1];
open (OUTPUT "> $file_name") or error("unable to create or open $file_name");
print OUTPUT "hello world";
close(OUTPUT);
command to execute:
perl perl_file.pl data.txt
it will work try