Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Failed to create a file

by Anonymous Monk
on May 06, 2011 at 02:15 UTC ( #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";

Comment on Failed to create a file
Download Code
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 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;

      You have made mistake is setting the path of the file where it will be create here it should be front slash /instead of \. See the following:

      #!/usr/bin/perl open(my $in, ">", "C:/Documents and Settings/admin/Local Settings/Te +mp/texti.txt") or die "Can't open license.txt: $!"

        it should be front slash /instead of \.

        Not at all. Windows accepts both slashes as directory separators, and "\" is actually the canonical form.

        PS — It's "forward slash".

        Khen1950fx is in fact correct. C: strongly implies a Windows system and Windows uses \ as the normal path separator. Most Windows applications and the system accept / as an alternative, but not everything.

        True laziness is hard work
Re: Failed to create a file
by ikegami (Pope) on May 06, 2011 at 07:12 UTC
Re: Failed to create a file
by jellisii2 (Friar) on May 06, 2011 at 13:05 UTC
    mkdir('C:/TEMP/cfg') or die 'Could not create folder';
Re: Failed to create a file
by MidLifeXis (Prior) 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

      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-11-20 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (101 votes), past polls