parthodas has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm trying to create a directory($mycrdir) & file(file_detail.txt) runitme. I'm using the below code to do so. Although, the file is not getting created when providing the directory name. If I remove the directory variable, the file is getting created, but in different directory. Please help me.
open my $OUT, '>>', "$mycrdir/file_detail.txt" or die "Can't create '$ +mycrdir/file_detail.txt'" ; while (my @row = $sth->fetchrow_array) { print $OUT @row, "\n"; } close $OUT;
The error is as below
Can't create 'D:\SiebelAdmin\CR_Files\CR117365_20150821_0022
/file_detail.txt' at sblmaint.pl line 5447, <> line 3.

Replies are listed 'Best First'.
Re: Creating a new file in a directory
by Corion (Pope) on Aug 21, 2015 at 07:27 UTC

    Is there a newline in $mycrdir? Also, you should output the reason why open fails:

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

    The error reason maybe gives you a better hint at what fails. Also try hardcoding $mycrdir in your script first to see whether whitespace or other stuff might be a problem.

      It seems a new line character comes at the end of $mycrdir and the file name goes to the next line. Maybe, because of this the file is not getting created. I chomped $mycrdir to remove any white characters, but then also it is failing.

        chomp does not remove all whitespace from the end of a string. I recommend that you be more explicit and use:

        $mycrdir =~ s!\s*$!!;

        Also, what is the error message your OS gives you in $! when creating the file fails?

Re: Creating a new file in a directory
by Monk::Thomas (Friar) on Aug 21, 2015 at 08:57 UTC

    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.

      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.

      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

Re: Creating a new file in a directory ( Path::Tiny )
by Anonymous Monk on Aug 21, 2015 at 07:28 UTC
    Path::Tiny
    $ perl -MPath::Tiny -e " path( q{goner/goner/goner/goner.txt} )->spew( +1234); " Error sysopen on 'goner/goner/goner/goner.txt37921829568512': No such +file or directory at -e line 1. $ perl -MPath::Tiny -e " path( q{goner/goner/goner/goner.txt} )->touch +path->spew(1234); " $ cat goner/goner/goner/goner.txt 1234 $ rm -rfv goner removed `goner/goner/goner/goner.txt' removed directory: `goner/goner/goner' removed directory: `goner/goner' removed directory: `goner'

      Aran! Aran!

      Sorry. Could not resist. (It's an offtopic reference / joke. Goner Aran)

        Aran! Aran! Sorry. Could not resist. (It's an offtopic reference / joke. Goner Aran)

        Not very apt or funny :)