Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

open vs. sysopen

by sheep (Chaplain)
on Dec 26, 2003 at 14:04 UTC ( #317088=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I have 2 lines that according to the documentation should be equivalent, yet one of them (open) works fine whereas the second (sysopen) refuses to behave!
... use Fcntl; ... $ggg =~ s/\s//g; # no spaces - just incase open(DEST, ">", $ggg) or $self->ascii_error("Cant upload files: $! Fil +e: $ggg"); # this works fine
The same code but with:
sysopen(DEST, $ggg, O_WRONLY | O_TRUNC | O_CREAT) or $self->ascii_erro +r("Cant upload files: $! File: $ggg");
doesn't work and here ascii_error() is called with $!: No such file or directory, and $ggg showing proper (as far as I can tell) file path+name.

The Camel Book says:

The following examples show equivalent calls to both functions.
(...)
Open a file for writing, creating a new file if needed, or truncating an old file:
open(FH, ">", $path); sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT);

I have no clue what I am doing wrong.
Any help appreciated.
-sheep

Replies are listed 'Best First'.
Re: open vs. sysopen
by oha (Friar) on Dec 26, 2003 at 17:04 UTC
    no idea, i humbly suggest to try open with O_RDWR flag set instead of O_WRONLY. I also suggest you to write down you OS, and the kind of fs you ar using (mounted?).

    more: do POSIX::open() works?

    this may give more information to fix it.

      Changing to O_RDWR doesn't help.
      OS: Slackware 9.1 Linux 2.4.23
      fs: /dev/hdd3 / ext2 rw 0 0
      POSIX::open() works fine
      Thanks!
      -sheep

        Perhaps you can supply some additional code, or a small test case that exhibits the behavior. The following two lines of code behave properly on my Slackware Linux box (2.4.18), assuming proper permissions on directories involved in the filepath.

        open(DEST,'>',$ggg) or die $!; sysopen(DEST, $ggg, O_WRONLY | O_TRUNC | O_CREAT) or die $!;
Re: open vs. sysopen
by melora (Scribe) on Dec 26, 2003 at 17:55 UTC
    Okay, it was just a thought (kind of thing I'd goof up). I played with sysopen a little (not use-ing anything at all) and this worked okay (I shifted $file from the command line, and it was simply sheep.txt):
    sysopen(FH, $file, O_WRONLY | O_TRUNC | O_CREAT); print FH "Whooyah";
    goshdammitforgot to use code brackets! It did write Whooyah to the file, so it worked all right. So maybe the trouble is in the $file string you're giving it, after all.
      I tried one or the other (commenting out one) not both at the same time.
      The "$ggg" file, as well as "DEST" filehandle is used only in this bit.
        Okay, it was just a thought (kind of thing I'd goof up). I played with sysopen a little (not use-ing anything at all) and this worked okay (I shifted $file from the command line, and it was simply sheep.txt): sysopen(FH, $file, O_WRONLY | O_TRUNC | O_CREAT); print FH "Whooyah"; It did write Whooyah to the file, so it worked all right. So maybe the trouble is in the $file string you're giving it, after all.
Re: open vs. sysopen
by Aristotle (Chancellor) on Dec 26, 2003 at 22:35 UTC

    I can't reproduce this on my fairly similar system.

    Are we talking about the same values of $ggg here for all invocations?

    Have you made sure the sysopen variant fails consistently? How many times did you try, under what circumstances? Was this the only thing you changed in your code and its environment (such as user ID etc) between test runs?

    I know these questions are fairly basic, and I didn't initially reply because I have no idea why it would fail. I was hoping that someone else would be able to pinpoint the issue. However, since there doesn't seem to be anything in your code that explains the failures you're experiencing, I guess we need to go looking elsewhere instead of guessing blindly (and badly (including not reading the question)).

    Makeshifts last the longest.

      1) $ggg is not the same (in some cases it is created randomly) for all invocation in the original code, however for debugging I changed it to a static name, and the error stays the same.
      2) Yes, sysopen() fails consistently.
      3) I tried it 100++ times.
      4) Yes, the only thing I changed was to comment out open() call and uncomment sysopen(). UID stays the same. Also, I made sure the directory had the right permissions.

      Thanks for the help.
      -sheep

        So nothing else interferes. In that case, there's not much else left to say but "it's (probably) a bug". :/ Now the question is is it a bug in Perl proper, per se - in which case you should take a look at perldoc perlbug -, or is it an issue with the Perl binary package in Slackware, in which case you'd have to report to Patrick or something. (There doesn't seem to be any anoynmous address @slackware.com for bug reports or such, oddly enough.)

        Makeshifts last the longest.

Re: open vs. sysopen
by sgifford (Prior) on Dec 27, 2003 at 06:49 UTC

    The first step I would try in debugging is using strace or truss to see what arguments are really being passed to the system open function. That should point you in the right direction.

    Although this theory has already been debunked, the behavior you're seeing is consistent with O_WRONLY, O_TRUNC, and O_CREAT being undefined or zero. You could try printing the results out before calling sysopen, just to make sure:

    print "Flags are ",O_WRONLY|O_TRUNC|O_CREAT,"\n";
    On my system, the result is 577.

    Also, you've tried the code with changing $self->ascii_error to just die, and with changing $ggg to a fixed value. Why don't you post the exact script you're using with these changes, in a way that any of us can just cut-n-paste to see if we get the same error (instead of making these same changes ourselves, possibly slightly differently than you did)?

Re: open vs. sysopen
by !1 (Hermit) on Dec 26, 2003 at 19:39 UTC

    Just curious: what does $ggg contain? Did it contain spaces prior? Does adding print "Exists$/" if -e $ggg; print exists? Your error is right there: No such file or directory. I'm very suspicious of $ggg.

      yes! i translate the open man page few days ago, perl do magic on filenames, stripping trailing spaces and so on.

      it do different things depending how open is called. maybe $ggg contains something which open can handle with this magic, but sysopen doesn't?

Re: open vs. sysopen
by xenchu (Friar) on Dec 26, 2003 at 18:30 UTC

    According to the book I am using the basic format for sysopen is:

          sysopen HANDLE, $filename, O_WRONLY|O_CREAT|O_TRUNC;

    Not a great difference I admit, but it may be significant. The sysopen examples I can find do not use parentheses between open command and filehandle. Easy enough to test at any rate. And you are not trying to use open and sysopen on the same file are you? (Didn't think so.)

    It would help if you would include the entire error message in your next post.

    HTH,

    xenchu


    The Needs of the World and my Talents run parallel to infinity.
      It would help if you would include the entire error message in your next post.
      I changed ascii_error() to die() and recieved the following error:
      ERROR: No such file or directory at /usr/lib/perl5/site_perl/CG/Binary.pm line 479.

      -sheep

      Easy enough to test at any rate

      And easy enough for you to test to find out that parentheses around the arguments are not a problem. You could also check the docs and notice that all the code examples in perlopentut, as well as the examples of sysopen in the open documentation *all* use parentheses.

      Perl is a programming language, not a guessing game. You might as well have asked if the OP walked around his chair three times chanting bits of the Katha Upanishad before running the code. Not a big difference, and easy enough to test right?

open vs. sysopen SOLVED (partially)
by sheep (Chaplain) on Dec 27, 2003 at 20:08 UTC
    Hello again,

    The problem is solved, but I have to confess I still don't know what was causing it.
    I followed sgifford's idea and oddingly got 513 as a result. On all systems, I got access to, the result was 577.

    As I had to finish the code and have it working by this afternoon, I gave up proper debugging and blindly reinstalled relevant modules. (I know, I know - shame on me!)
    Fresh Fcntl.pm install fixed my problem.
    It wasn't an issue with the Perl binary package in Slackware - default package was removed and Perl 5.8.2 was installed from the source code.
    I cannot guarantee nobody was messing with Fcntl.pm module (not only I have access to this system), but it seemed to me so improbable and absurd that I didn't bother with checking it.

    Anyhow, I would like to thank all of you for your help and ideas, and also say sorry for not fixing it the proper way finding the source of the problem.

    Unfortunatelly this time (even though the problem is fixed):
    sheep - perl : 0 - 1
    But I will see you again perl!

    -sheep

      Not to re-open an old wound, but I ran across something this weekend which might be relevant: I was porting a Perl script which I wrote from Win32 systems to both MacPerl and to FreeBSD, and found that my open calls, such as
      open(FH, '>', 'Myfile.txt');
      needed to be rewritten as
      open (FH, '>Myfile.txt');
      The error message I was getting was "too many arguments", so this may be something entirely different from the above problem, but it did make me come back and have a look at this thread. Anyway, here it is, just in case it helps someone.

        Sorry? Are you saying that you couldn't use the 3-arg open on Win32?

        If so, could you post an example program that demonstrates that.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: open vs. sysopen
by jsprat (Curate) on Dec 26, 2003 at 21:37 UTC
    I'd guess that there's no use POSIX; in the snippet. POSIX.pm is where the O_WRONLY, O_TRUNC and O_CREAT constants are defined.

    If my guess is right, you also need to use strict; and use warnings; (see Use strict warnings and diagnostics or die for a good discussion of this), which would point you to the real error - the barewords, O_*.

    Update: Strike obviously incorrect info

      I do use both: use warnings; and use strict;.
      Even though I was using $! to get the error message, I changed ascii_error() to die() supplying the full error in a reply to one of the posts above. As to the rest of the suggestions the previous reply to your post explains me.

      Thanks,
      -sheep

      I'd guess that there's no use POSIX; in the snippet. POSIX.pm is where the O_WRONLY, O_TRUNC and O_CREAT constants are defined.

      A poor guess. The code shown in the original post clearly has use Fcntl, which by default will export all system F_* and O_* constants.

Re: open vs. sysopen
by TomDLux (Vicar) on Dec 27, 2003 at 15:13 UTC

    Does $ggg contain anything?

    Does $ggg specify a file which exists? Or just something similar to the name of a file that exists.

    Are you looking in the correct directory?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2021-11-29 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?