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


In reply to Re: file open with variables by kcott
in thread file open with variables by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.