in reply to file open with variables

G'day Steve,

Welcome to the Monastery.

Using the strict pragma is good; use it in all of your Perl code. You should also use the warnings pragma in all code.

In this instance, I would also use the autodie pragma. Hand-crafting your own I/O exception messages is tedious and notoriously error-prone. In your code, the message won't tell you which file had a problem; compare with the output from my example code below, which I wrote to intentionally fail, but I didn't need to add 'or die "some message: $!"'.

You need to start your variables with a sigil; e.g. $X_info instead of X_info. If you'd run your posted code, you would have been alerted to this. For future reference, please do basic checks on code before posting.

From your question, I get the impression that you want to open different files based on the supplied variables. Use a subroutine to do this. See my code below for an example of this; the open function explains the preferred 3-argument form and use of a lexical filehandle.

I wasn't certain exactly what replacements in the filename you wanted. I've just shown an example; modify to suit your needs. Also, I've used a single hash instead of three separate variables; that's just to show an alternative method which may, or may not, be useful for you.

$ perl -e ' use strict; use warnings; use autodie; my %info = qw{X 3 Y -4 Z 5}; handle_csv_file(@info{qw{X Y Z}}); sub handle_csv_file { my ($X, $Y, $Z) = @_; my $csv_file = sprintf q{D:\PROJ\%s\%s\%s.csv}, $X, $Y, $Z; open my $fh, "<", $csv_file; # do something with $fh here return; } ' Can't open 'D:\PROJ\3\-4\5.csv' for reading: 'No such file or director +y' at -e line 14

For future use, consider creating an account. This is very easy to do and doesn't require you to provide lots of personal information; see "Create A New User". You gain a number of benefits and it differentiates your posts from all of the other anonymous posts (currently >100,000).

— Ken