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

I am reading a binary file and I am using the tell() function to get the specific position of a location. I am reading the file in binary mode:

open( FH , "+<" , $source ) or die "Can not open file: $source $!\n"; binmode(FH);

I am moving the pointer to the desired position with string condition and using:

$position = tell(FH)

In order to get the pointer position int. Then I use SEEK to move to the position that I have previously marked by doing:

seek( FH , $position , SEEK_SET );

Where I am issuing the commang:

write( FH, $example , $example_size ) or die "Couldn't write at FH : $!\n";

I was browsing over the internet and I found that the opposite of read() is not write(). But later on trying to find a solution to my problem I came across with the Perl man page perlapio − perl’s IO abstraction interface. and also perlclib.

Both of the web pages state that the alternative of fwrite() in C is write():

fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes)

I tried to apply that in my code and I keep getting this error:

Useless use of a constant (FH) in void context at test.pl line 193.

I looked online what is the error and I found that it can not open the file properly. I do have a die condition on open and also I have a few printf() conditions to test if the file is opening and they are coming up. So at this point I assume it is a wrong syntax with the write() function. Are there any alternatives that I could use?

Thanks in advance for your time and effort.

Replies are listed 'Best First'.
Re: Writing at specific position and length in binary file.
by wazat (Monk) on Jan 11, 2014 at 04:37 UTC

    as no_slogan points out, use print.

    write is used for printing formatted text.

    The opposite of read() is print().

    To further confuse things, the functions sysread() and syswrite() are indeed opposite. They are the low level (unbuffered) versions of read() and print().

      To: wazat, a start reading about sysread() and syswrite() and it looks like a good solution only in cases the program does not include seek() tell() etc. Unfortunately in my case these functions are necessary. It is nice to know that there are alternative solutions for future use. Thank you for your time and effort.

      I am new user to this cite and I have not figure out how to vote answers, it is not so clear to me. Can you point me to right direction to read the documentation thanks again. Best Regards.

        You can use sysseek with sysread and syswrite. There's also a workaround for tell.

        But, you probably still don't want to use the sys* functions, because they don't do any buffering.

        Also, you may like to read about the Voting/Experience System.

Re: Writing at specific position and length in binary file.
by no_slogan (Deacon) on Jan 11, 2014 at 02:38 UTC
    Just use print FH $example.

      To: no_slogan, I guess this is the simplest and best solution to my problem. I had to do a small modification to my code by writing initially to that point a string with "\0" positions. The reason is that the name can vary and in case of rewriting on top of the previous they might print together with remaining characters. So had to came up with this "small" trick. Thank you again for your time and effort.