Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Creating a new file in a directory

by Monk::Thomas (Friar)
on Aug 21, 2015 at 08:57 UTC ( [id://1139395]=note: print w/replies, xml ) Need Help??


in reply to Creating a new file in a directory

Suggestion:

instead of creating the filename twice, like in

open my $OUT, '>>', "$mycrdir/file_detail.txt" or die "Can't create '$ +mycrdir/file_detail.txt'" ;

it is better do it once and use a variable

my $filename = "$mycrdir/file_detail.txt"; open my $OUT, '>>', $filename or die "Can't create '$filename'";

The reason is to avoid any chance of updating the filename used for open() but forgetting about the error message.

one more improvement - include the reason why the open failed

open my $OUT, '>>', $filename or die "Can't create '$filename': $!";

P.S.: You are using UNIX-Style forward slashes on a Windows-Platform: D:\.../file_detail.txt You probably want to fix that.

Replies are listed 'Best First'.
Re^2: Creating a new file in a directory
by RonW (Parson) on Aug 21, 2015 at 19:01 UTC
    P.S.: You are using UNIX-Style forward slashes on a Windows-Platform: D:\.../file_detail.txt You probably want to fix that.

    The Windows "kernel" accepts forward slashes as well as back slashes as directory delimiters. The forward slash is usually only a problem with command line applications (and cmd.exe) which expect options to be introduced by forward slash.

    However, Unix, Linux and related OSs do not accept back slash as directory delimiters. Also, there are other OSs use other characters and do not accept either back or forward slashes as directory delimiters.

      This (and Anonymous') is a typical technical answer. Totally correct but both of you fail to consider the actual user. The filename is presented to the user as part of an error message. Wouldn't it be a lot more user friendly if the program shows the filename in native format? That way the user doesn't have to concern itself with what's going on behind the scenes.

      Also, there are other OSs use other characters and do not accept either back or forward slashes as directory delimiters.

      I am totally aware of that and I am quite fond to let Path::Class handle that for me. However it's a bit besides the point. The OP did not imply that the program was running on any other OS besides Windows.

Re^2: Creating a new file in a directory
by Anonymous Monk on Aug 21, 2015 at 09:19 UTC

    Monk::Thomas : P.S.: You are using UNIX-Style forward slashes on a Windows-Platform: D:\.../file_detail.txt You probably want to fix that.

    Perfectly normal win32 feature

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-19 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found