Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

opening filehandle with Win32API::File seems to return scalar

by Lotus1 (Vicar)
on Oct 14, 2014 at 20:39 UTC ( [id://1103814]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to understand Win32API::File with the hopes of eventually doing locking on a text file in Windows Server 2008. My first test of some example code gives an error that makes me think createFile() is returning a scalar instead of a Win32 native file handle.

use strict; use warnings; use Data::Dumper; use Win32API::File 0.08 qw( :ALL ); my $hObject = createFile( "test.xml", 'rw k e' ); unless ( $hObject ) { die "error creating handle: $^E\n"; } print Dumper( $hObject); tie my $FILE, 'Win32API::File', $hObject; ###line 13 print $FILE "test"; print "finished\n"; __DATA__ output: Can't locate method Win32API::File::TIESCALAR via package Win32API::Fi +le at D:\Users\\testlock\t1.pl line 13 $VAR1 = 92;

According to the documentation for createFile "... $hObject gets set to a Win32 native file handle ..."

The example code has a comment that says "It also supports tying via a win32 handle (for example, from createFile()):"

My question is what am I doing wrong here? I'm using ActiveState perl 5.16 and the Win32API::File module is version 0.1201.

Replies are listed 'Best First'.
Re: opening filehandle with Win32API::File seems to return scalar (OsFHandleOpen)
by tye (Sage) on Oct 14, 2014 at 20:44 UTC

    Pass the Win32 native handle to OsFHandleOpen() to get a Perl file handle.

    - tye        

      I got it to work that way but only with a baretext filehandle and without using strict. When I used a lexical variable I got the error message "Can't use an undefined value as a symbol reference at D:\Users\lock\t2.pl line 18." Line 18 is the print statement.

      I still don't understand why the tie didn't work. There was no implementation of TIESCALAR in the module but I found TIEHANDLE.

      #use strict; use warnings; use Data::Dumper; use Win32API::File 0.08 qw( :ALL ); my $testfile = "test.xml"; print "file exists\n" if -e $testfile; my $hObject = createFile( $testfile, 'rw k e' ); unless ( $hObject ) { die "error creating handle: $^E\n"; } print Dumper( $hObject); my $fh; OsFHandleOpen( FHTEST, $hObject, 'rw' ) or die "OsFHandleOpen: $^E\n"; print FHTEST "test"; print "finished\n"; __DATA__ output: Name "main::FHTEST" used only once: possible typo at D:\Users\lock\t2. +pl line 18. file exists $VAR1 = 116; finished
        OsFHandleOpen( my $fh = IO::Handle->new(), $hObject, 'rw' ) or die ...

        It would actually have a saner interface if it weren't emulating open's weird interface. And it has yet to adopt the new idea of open autovivifying when passed undef instead of a handle (though I recall coding that improvement long ago, just never got released, it seems).

        - tye        

Re: opening filehandle with Win32API::File seems to return scalar
by RonW (Parson) on Oct 14, 2014 at 21:00 UTC

    FYI, on Linux, Unix and similar systems, not only does the "raw" open service return a scalar, said scalar is just an integer. Usually applications don't use the raw open (or other raw file IO services) directly. Rather they use a library that provides a "friendlier" interface for file IO.

    Perl has it's own file IO layer, as well as sysopen, sysread, etc, which provide access to the raw file IO services (or a POSIX-ized, semi-raw layer on top of non-POSIX OSs like MS Windows.)

      I read the documentation for sysopen and the only thing it seems to have that open doesn't is the ability to specify the permissions of newly created files. My old Programming Perl book says to use sysopen instead of open() before using flock() but I see the example at flock uses open(). Is there something else I missed?

      In the OP I was attempting to combine two lines of example code from the module documentation that don't work as expected. The documentation says you can tie a filehandle to a win32 handle created by createFile(). I tried that and got an error message about a missing method Win32API::File::TIESCALAR. I found a method called Win32API::File::TIEHANDLE and I was surprised that it wasn't the one called. I was expecting the low level filehandle to be something more than an integer. As you can see in the OP I printed the value of the filehandle and it was an integer.

      I wasn't interested in flock since text editors can still open and wipe out the edits my application is making while it is running. I've noticed that when certain types of files are open in Excel I can't overwrite them. I'm trying to get that same low level locking on a Windows Server 2008 platform.

      ...Usually applications don't use the raw open (or other raw file IO services) directly. Rather they use a library that provides a "friendlier" interface for file IO.

      It was my understanding that Win32API::File is a Perl friendly interface to the Win32 API. Saying that people usually don't use raw open is one of those true but irrelevant statements. If raw open is what is needed then people use raw open would be my guess.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found