Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Trying to Create and Write to a file

by IPstacks (Novice)
on Apr 07, 2003 at 16:39 UTC ( [id://248668]=perlquestion: print w/replies, xml ) Need Help??

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

Well, it's question time again... I have the following code:
#!/usr/bin/perl -w use strict; # Modules to use use Config::IniFiles; # Variables my ($CorE, $PlugInDir, $LogFile, $ForkLimit, $TftpRoot, $TftpPath, $Fi +leStyle, $DateStyle, $PreCmd, $PostCmd, $SNMPver, $SNMPretry, $SNMPwait, $SNMPm +tu, $SNMPdebug, $SNMPCommunity, $TFTPserver); # Start print 'Would you like to edit the current pancho configuration file or Create a new file [e/C]? ';$CorE=<STDIN>; # Create new file if ($CorE eq 'C') { open (CREATE,">>pancho.conf"); print CREATE "[Global]"; print 'What is the IP address of your TFTP server? '; $TFTPserver=<STDIN>; print CREATE $TFTPserver; close (CREATE); }
What I am trying to do is create a file when the user enters "C" as their answer. Then when it creates the file it will start the file heading with "Global" and all the user to then enter the ip address of their TFTP server. The script should then write their tftp server ip address to the file, close the file and then exit. However, what I have doesn't make anything in the if loop run. I enter "C" as my choice and then it just dies. I would appreciate any help you can offer.

Replies are listed 'Best First'.
Re: Trying to Create and Write to a file
by c (Hermit) on Apr 07, 2003 at 16:45 UTC
    I'm guessing that you'll need to chomp the value of $CorE after you've read it in from STDIN. You're trying to match on just the value 'C', when perl is probably seeing 'C\n' as its value.

    -c

      That did the trick, thanks c

        Just as a suggestion, you may also want to consider ignoring the case of the input. For example if someone enters 'c' then it doesn't pass the conditional. You could use a simple regex such as if ($CorE =~ /^C/i) which would also eliminate the need for chomping.

Re: Trying to Create and Write to a file
by Improv (Pilgrim) on Apr 07, 2003 at 16:51 UTC
    Change your open() call from
    open (CREATE,">>pancho.conf");
    to
    open(CREATE, ">pancho.conf") || die "Could not open pancho.conf: $!\n" +;
    Notice that I changed the >> to just a > -- what you've said suggests that appending is less appropriate than truncating and then writing.
Re: Trying to Create and Write to a file
by Coplan (Pilgrim) on Apr 07, 2003 at 17:16 UTC
    Just a quick comment.

    Whenever I'm using "if" loops, I always make sure to place an "else" as it helps with tracing. Especially in a situation like this, you would at least know that there wasn't a match:

    if ($omething) { # do stuff } else { print "There was no match!\n"; # or some other error }

    You could easily place in a more intuative error message. Even if this is only temporary to help you test the code, it's a worthwhile practice to get into.

    --Coplan

Re: Trying to Create and Write to a file
by Juerd (Abbot) on Apr 07, 2003 at 16:48 UTC

    open (CREATE,">>pancho.conf"); print CREATE "[Global]"; close (CREATE);

    Could be anything. You're not checking the return values of these file related functions.

    Remember:
    open or die
    print or die
    close or die

    This goes for many other functions as well.

    open CREATE, '>>pancho.conf' or die $!; print CREATE '[Global]' or die $!; close CREATE or die $!;

    You probably should be more verbose than just $!, but don't forget to include $!, since it contains the actual error message.

    That said, you should learn Perl. Try reading a book.

    Juerd
    - http://juerd.nl/
    - spamcollector_perlmonks@juerd.nl (do not use).
    

      I have to offer apologies -- I originally was going to vote this note up based on the checking of errors. Then reconsidered based on the last line, and missed the null vote.

      So my vote should have been null, and I actually voted --.

        So my vote should have been null, and I actually voted --.

        No need to apologize. Votes are anonymous, and I couldn't care much less ;) One wrong vote won't hurt anyone.

        Juerd
        - http://juerd.nl/
        - spamcollector_perlmonks@juerd.nl (do not use).
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-20 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found