Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

open file does not work for externally defined filename

by cristianekw (Novice)
on Jan 07, 2008 at 20:22 UTC ( [id://660928] : perlquestion . print w/replies, xml ) Need Help??

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

Hi, I have a strange problem here. Using getoptions, I get and store a file name. After some operations, I get this string to append something to this file. Using open command, I can read it, but I canīt write in it. If I use open(FH,">>$file") it does not work. But it works fine If I use open(FH,">>c:\\logfile.log"). I thought it could be a slash problem, but if I call the program with any number of slash, the problem persists. Am I making something wrong? Thanks for any help.
  • Comment on open file does not work for externally defined filename

Replies are listed 'Best First'.
Re: open file does not work for externally defined filename
by kyle (Abbot) on Jan 07, 2008 at 20:25 UTC

    Let Perl tell you what the error is.

    open my $fh, '>>', $file or die "Can't append to '$file': $!";

    Note the use of three argument open and $! to get the error (see perlvar).

Re: open file does not work for externally defined filename
by Corion (Patriarch) on Jan 07, 2008 at 20:25 UTC

    What is the error message you get when opening your file?

    open( FH, ">>$file") or die "Couldn't open '$file' for output: $!";

    Also, what is the value of $file?

Re: open file does not work for externally defined filename
by rafl (Friar) on Jan 07, 2008 at 20:26 UTC

    When a call to open() fails it returns undef and sets the special variable $!, which will allow you to find out what exactly is going wrong.

Re: open file does not work for externally defined filename
by derby (Abbot) on Jan 07, 2008 at 20:28 UTC

    Can you post more code ... like how $file is set? Also, try checking the return value of the open call:

    open( FH, ">>", $file ) or die "Cannot open $file: $!\n";

    ... but if I call the program with any number of slash ...

    As MJD says you can't just make shit up and expect the computer to know what you mean (Good 'ole #11901)

    -derby
Re: open file does not work for externally defined filename
by webfiend (Vicar) on Jan 07, 2008 at 21:21 UTC

    Just for the sake of completeness ... try Fatal if you're opening lots of files or maybe just feel like writing different boilerplate:

    use Fatal qw(open close); #... open(FH, ">>c:\\logfile.log"); #..
Re: open file does not work for externally defined filename
by FunkyMonk (Chancellor) on Jan 07, 2008 at 20:30 UTC
    perl will tell you what it thinks the problem is if you add or die $! on to the end of your open statements:
    open(FH,">>$file") or die $!; open(FH,">>c:\\logfile.log") or die $!;

    If they don't help you sort out your problem, you will have to show us some more code before we can help you further

    PS

    wrapping your code in <code> ... </code> tags will format it just like mine above.

Re: open file does not work for externally defined filename
by cristianekw (Novice) on Jan 08, 2008 at 10:46 UTC
    It was an eclipse error... Sorry... I'm using EPIC to develop in PERL. EPIC does not run very well in some cases... :( Does anyone know a better plug-in or editor?

      You should try to run your code outside of EPIC or whatever environment you use, especially when you are hunting errors.

      You could also have told us what error you get in EPIC, but it's far more fun to keep us guessing!

        This is the problem: it does not show any error!!! Just freeze!