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

I created a variable $outputFileName.

The value of $outputFileName is /home/myung/ns-allinone-2.28/ns-2.28/COMP_TC/result/RW/K9-RW.

Except the file name itself (K9-RW), all directories exist.

I want to create a file called "K9-RW" and write to it.

So I did programming as follows.

sysopen(OUTPUT_HANDLE, $outputFileName, O_WRONLY|O_APPEND|O_CREAT, 060 +0) or die " Can't open $outputFileName : $!";
But I got an error like the below.

[myung@mirim script]$ conn.pl 100 1 2 9 Can't open /home/myung/ns-allinone-2.28/ns-2.28/COMP_TC/result/RW/K9-R +W : No such file or directory at ./conn.pl line 128.
what is wrong with my coding? Please help me out. Thank you.

Replies are listed 'Best First'.
Re: What is wrong with my File open?
by sauoq (Abbot) on Nov 19, 2005 at 02:49 UTC

    It is very likely that you are simply failing to use Fcntl, which is where your constants (O_CREAT, etc.) are defined. If you don't already have it, put

    use Fcntl;
    somewhere (probably near the top) in your script and that should take care of it for you.

    Update: Here's a demo showing that this is possibly the problem:

    500,0 sauoq@fozzie:~$ perl -e 'sysopen(OH, "./foo", O_CREAT) or die $! +' No such file or directory at -e line 1. 501,2 sauoq@fozzie:~$ perl -MFcntl -e 'sysopen(OH, "./foo", O_CREAT) o +r die $!' 502,0 sauoq@fozzie:~$

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thank you so much your replies.
      Since I am new to PERL, I did not know I needed to put "use Fcntl".
      Now, it is working fine!
      Thanks, all of you.
      Myung
        Perl, not PERL.

        Can you articulate why you are using sysopen instead of open?

Re: What is wrong with my File open?
by dave_the_m (Monsignor) on Nov 19, 2005 at 00:00 UTC
    Except the file name itself (K9-RW), all directories exist.
    I would strongly suspect this is not in fact the case - possibly due to subtle typos, or differences in case, or control characters or spaces. Try adding the following check on the line before the sysopen:
    while ($outputFileName =~ /.*?\//g) { die "no dir: $`$&\n" unless -d "$`$&"; }

    Dave.

Re: What is wrong with my File open?
by runrig (Abbot) on Nov 19, 2005 at 00:11 UTC
    See if this tells you anything:
    chdir "/" or die "Can't cd to /: $!"; my @dir = split /\//, $outputFileName; pop @dir; shift @dir; chdir $_ or die "Can't cd to [$_]: $!" for @dir;