in reply to Re: simply appending to a scalar...
in thread simply appending to a scalar...

The example of the open function you've mentioned has become of some interest also.

I am able to open and print to the in-memory filehandle, though unable to syswrite to it and i don't know why :(

thank you for your patience,


Replies are listed 'Best First'.
Re^3: simply appending to a scalar...
by davido (Cardinal) on Jun 28, 2006 at 04:00 UTC

    Because syswrite operates at the system level, bypassing Perl's higher-level file IO.

    Let me just say this however: Of the seven ways I listed for appending information to a string, in-memory filehandles are the most difficult, least Perlish one; the one that I strongly urge you to consider not using. There are three ways down from the top floor of the Sears Tower building: You can take the elevator, you can take the staircase, or you can jump off the roof. I would advise against jumping off the roof. But doing so will get you to the bottom, pronto.

    There is a very limited set of problems for which using in-memory filehandles will be the optimal choice. ...very limited, and you rarely see them in run-of-the-mill everyday code. It's a little like symbolic references (though even less useful); they exist, they have uses, but you will rarely see them in code, and will probably never actually need to implement them yourself.

    You may have one of those fairly uncommon situations where the in-memory filehandle leads to better code. If you do, I'd love to see what that application is. Then the next time someone asks where it's a good idea to use them, I'll have at least one good example. lol


Re^3: simply appending to a scalar...
by ikegami (Patriarch) on Jun 28, 2006 at 04:48 UTC
      Ah, is it an absolute rule not to use open with sysread/syswrite ?
      The reason I ask is that i've had no obvious problems before using something like :

      open(HANDLE,"+>$name") or die $!;

      Then say a four way select() & sysread() until sysread returns 0. Same can be said for syswrite() but without the select() and zero byte check.

      I'm aware that print and read are buffered, but i'm aware of little more than that ;-)

      To conclude, i reckon i will implement what was first decided on Sunday, and that is using sysread() to fill up the target scalar, it has been interesting however to have seen the other options, TMTOWTDI indeed.



        I could be wrong. I can't find anything in the documentation saying you can't use sys* on a filehandle opened with open. Maybe it works on some systems but not on others (like sockets requiring the use of the system functions). Maybe it's a convention I adopted to be on the safe side.

        However, you definitely can't intermiggle buffered and system file operations. For example, I can across a piece of code that used select, eof and sysread. It didn't work because sysread always returned 0. eof would move the data from the pipe into the buffer. After changing sysread to read, everything worked fine.