Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Failed to create a file

by Anonymous Monk
on May 06, 2011 at 02:15 UTC ( [id://903285]=perlquestion: print w/replies, xml ) Need Help??

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

Trying to create a file in temp folder using the below line of code,always get "Failed to create cfg".what am I dong wrong?

open(CFG, "C:\\TEMP\\cfg") || die "Failed to create cfg";

Replies are listed 'Best First'.
Re: Failed to create a file
by wind (Priest) on May 06, 2011 at 02:43 UTC

    Read the documentation for open. You're currently trying to open for reading instead of writing.

    Also, give yourself all the debugging information possible in your die statements. It would've told you what was wrong.

    my $file = "C:\\TEMP\\cfg"; open CFG, '>', $file or die "failed to open $file: $!";
Re: Failed to create a file
by ikegami (Patriarch) on May 06, 2011 at 07:12 UTC
Re: Failed to create a file
by Khen1950fx (Canon) on May 06, 2011 at 03:20 UTC
    I don't have Windows, so this is untested:);
    #!perl use strict; use warnings; open STDOUT, '>', 'C:\\TEMP\\cfg' or die "Failed to create cfg"; print "Does this work?\n"; close STDOUT;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Failed to create a file
by MidLifeXis (Monsignor) on May 06, 2011 at 13:09 UTC

    Slightly off topic, but related to coding style of sorts.

    open(CFG, "C:\\TEMP\\cfg") || die "Failed to create cfg";

    You have included the parens in your call to open, which allows the ... || die ... to work as expected. If you leave off the parens - open CFG, "C:\\TEMP\\cfg" || die "Failed to create cfg";, this will give you the wrong results. Having stubbed my toe on this many times, I have made it my habit to use action or die instead of action || die.

    --MidLifeXis

      An even alternativer solution to this is to use pjf's great autodie module, which avoids the issue altogether...


      What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
      Having stubbed my toe on this many times, I have made it my habit to use action or die instead of action || die.
      An alternative reaction is to always use parens on function calls: some_function(ARGS) || die. The advantage of this strategy is that it improves readability, plus now you don’t have to memorize a bunch of confusing and conflicting precedence rules.
        I'm curious, which precedence rules are "conflicting"?

        Also I find the rule "logical operators that are spelled out as words have loose precedence" not too hard to remember.

Re: Failed to create a file
by jellisii2 (Hermit) on May 06, 2011 at 13:05 UTC
    mkdir('C:/TEMP/cfg') or die 'Could not create folder';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found