Can't open file with $variable in filename

R

Robert

Hi,

I am having trouble opening a file where a variable is used in the file name
reference. Here is the non functional code:

my $location = "smith";
open (FH, "<../clients/$location/overview.txt") or die "Can't open the file:
$!";
my @overview = <FH>;
close(FH);
print @overview;

This code DOES work:

#my $location = "smith";
open (FH, "<../clients/smith/overview.txt") or die "Can't open the file:
$!";
my @overview = <FH>;
close(FH);
print @overview;

Above I manually added the directory name to find the file and commented out
the first variable definition. I beleive that somehow I need to distinguish
$location from the rest of the file name location but am not sure how that
is accomplished. I tried: open (FH, "<../clients/$location\/overview.txt")
but that didn't work either. TIA!

Robert
 
G

Gunnar Hjalmarsson

Robert said:
I am having trouble opening a file where a variable is used in the
file name reference.

I beleive that somehow I need to distinguish $location from the rest
of the file name location but am not sure how that is accomplished.

Normally you use braces for that:

"<../clients/${location}/overview.txt"

but since the character following the variable name isn't alphanumeric,
braces *shouldn't* be necessary.
I tried: open (FH, "<../clients/$location\/overview.txt") but that
didn't work either.

AFAIK, everything you mentioned *should* work.
 
J

Joe Smith

Robert said:
I am having trouble opening a file where a variable is used in the file name
reference. Here is the non functional code:

my $location = "smith";
open (FH, "<../clients/$location/overview.txt") or die "Can't open the file:
$!";

Works fine for me.

First off, make sure that you are really using the filename you
think you are.

my $location = "smith";
my $filename = "../clients/$location/overview.txt";
open(FH,"<$filename") or die "Can't open the file $filename: $!";

-Joe
 
A

Anno Siegel

Joe Smith said:
Works fine for me.

First off, make sure that you are really using the filename you
think you are.

my $location = "smith";
my $filename = "../clients/$location/overview.txt";
open(FH,"<$filename") or die "Can't open the file $filename: $!";

If this is the only place the filename is needed, I prefer not to
introduce an extra variable for it. A one-shot "for" is an alternative:

open FH, $_ or die "Can't read '$_': $!" for
"../clients/$location/overview.txt";

Anno
 
J

Joe Smith

Anno said:
If this is the only place the filename is needed, I prefer not to
introduce an extra variable for it. A one-shot "for" is an alternative:

open FH, $_ or die "Can't read '$_': $!" for
"../clients/$location/overview.txt";

I like that.
-Joe
 
A

Anno Siegel

Joe Smith said:
I like that.

There is a little snag (isn't there always). When using a lexical
variable for the filehandle, you can't declare it right there in the
open statement, as in

open my $fh, $_ or die "Can't read '$_': $!" for '/some/file'; # wrong!

This results in an undefined value in $fh without a warning. The
declaration must be outside the for-modified statement:

my $fh;
open $fh, $_ or die "Can't read '$_': $!" for '/some/file';

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top