Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found