Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: file open with variables

by choroba (Cardinal)
on Sep 27, 2022 at 16:56 UTC ( [id://11147136]=note: print w/replies, xml ) Need Help??


in reply to file open with variables

Single quotes don't interpolate variables. You need double quotes.
open(DLOG, '<', "D:\\PROJ\\N12${X_info}_X$X_info\\dataInfo_X${Y_info}_ +Y${Z_info}_decode.csv") or die "we have a problem: $!"; # ^

Note that I needed to add the comma after the mode, it's not optional.

Also, a backslash is special in double quotes, so it needs to be backslashed to keep its literal meaning.

Moreover, a variable name can't be followed by an underscore (or any other character valid in a variable name) in double quotes, e.g. "$x_" means the variable named $x_, not $x followed by an underscore; you might need to use the curly braces syntax "${x}_".

Even better, use sprintf:

open(DLOG, '<', sprintf 'D:\PROJ\N12%s_X%s\dataInfo_X%s_Y%s_decode.csv +', $X_info, $X_info, $Y_info, $Z_info) or die "we have a problem: $!" +; # ~~ ~~ ~~ ~~

The usual rant about using lexical filehandles will come shortly.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: file open with variables
by afoken (Chancellor) on Sep 27, 2022 at 17:15 UTC
    a backslash is special in double quotes, so it needs to be backslashed to keep its literal meaning.

    In this special case, a DOS-style path on Windows not passed to legacy applications, using forward slashes instead of backslashes also works and is slightly more readable:

    open(DLOG, '<', "D:/PROJ/N12${X_info}_X$X_info/dataInfo_X${Y_info}_Y${ +Z_info}_decode.csv") or die "we have a problem: $!";

    (Explaining the forward slashes: The entire Windows API accepts both forward slashes and backslashes, and so did DOS. Only a few legacy commands use forward slashes to introduce switch parameters. Perl does not care at all about forward slashes or backslashes in path names, it just passses the path name to the underlying operating system. See Re^2: What is the meaning of this line in Perl on linux? and the links in that subthread for more details.)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: file open with variables
by eyepopslikeamosquito (Archbishop) on Sep 28, 2022 at 07:49 UTC

    The usual rant about using lexical filehandles will come shortly

    So as not to disappoint the choroba: always use lexical filehandles!!! (and the 3-argument form of open).

    Further to kcott's exemplary sample code, which uses the recommended 3-argument form of open with a lexical filehandle, note that lexical filehandles are automatically closed at end of scope (used here to illustrate how Perl garbage collection differs from Java).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147136]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found