Re: passing Apache::File to XS code that expects FILE *?

2000-05-19 Thread Doug MacEachern

On Thu, 18 May 2000, Vivek Khera wrote:

  "DM" == Doug MacEachern [EMAIL PROTECTED] writes:
 
 DM On Wed, 17 May 2000, Matt Sergeant wrote:
  Well, this may be true, but if you load IO::File before startup then it's
  not too big a deal...
 
 DM but it still adds a great deal of bloat to the server.  and it's oo
 DM interface, while very slick, adds quite a bit of runtime overhead, turn
 DM the sugar sour imo.
 
 In an embedded environment like mod_perl, then how do you suggest to
 deal with the dangling file handles problem?  That is, I'm processing
 a file or two, and some error occurs.  In a normal perl program, I'd
 exit or return out and then when the program terminates, it
 automagically closes all the files.  In mod_perl, the auto-close
 doesn't happen until much later.  With the OO interface, when the
 handle goes out of scope, such as a function call return, the file is
 implicitly closed.
 
 What other mechanism do you propose to handle this situation other
 than IO::File?  I use it all the time myself.

in addition to stas' hints, even local *FH does the job, e.g.:

#/dev/null so strace output is more readable
open my $fh, "/dev/null";
select $fh;
$| = 1;

{
 print "enter";
 local *FH;
 open FH, $0;
 print "leave"
}

print "done";

% strace ~/test/io.pl
write(3, "enter", 5)= 5
- open("/home/dougm/test/io.pl", O_RDONLY) = 4
fstat(4, {st_mode=S_ISGID|S_ISVTX|0401, st_size=0, ...}) = 0
fcntl(4, F_SETFD, FD_CLOEXEC)   = 0
write(3, "leave", 5)= 5
- close(4)= 0
write(3, "done", 4) = 4





Re: passing Apache::File to XS code that expects FILE *?

2000-05-18 Thread Vivek Khera

 "DM" == Doug MacEachern [EMAIL PROTECTED] writes:

DM On Wed, 17 May 2000, Matt Sergeant wrote:
 Well, this may be true, but if you load IO::File before startup then it's
 not too big a deal...

DM but it still adds a great deal of bloat to the server.  and it's oo
DM interface, while very slick, adds quite a bit of runtime overhead, turn
DM the sugar sour imo.

In an embedded environment like mod_perl, then how do you suggest to
deal with the dangling file handles problem?  That is, I'm processing
a file or two, and some error occurs.  In a normal perl program, I'd
exit or return out and then when the program terminates, it
automagically closes all the files.  In mod_perl, the auto-close
doesn't happen until much later.  With the OO interface, when the
handle goes out of scope, such as a function call return, the file is
implicitly closed.

What other mechanism do you propose to handle this situation other
than IO::File?  I use it all the time myself.



Re: passing Apache::File to XS code that expects FILE *?

2000-05-18 Thread Matt Sergeant

On Thu, 18 May 2000, Vivek Khera wrote:

  "DM" == Doug MacEachern [EMAIL PROTECTED] writes:
 
 DM On Wed, 17 May 2000, Matt Sergeant wrote:
  Well, this may be true, but if you load IO::File before startup then it's
  not too big a deal...
 
 DM but it still adds a great deal of bloat to the server.  and it's oo
 DM interface, while very slick, adds quite a bit of runtime overhead, turn
 DM the sugar sour imo.
 
 In an embedded environment like mod_perl, then how do you suggest to
 deal with the dangling file handles problem?  That is, I'm processing
 a file or two, and some error occurs.  In a normal perl program, I'd
 exit or return out and then when the program terminates, it
 automagically closes all the files.  In mod_perl, the auto-close
 doesn't happen until much later.  With the OO interface, when the
 handle goes out of scope, such as a function call return, the file is
 implicitly closed.
 
 What other mechanism do you propose to handle this situation other
 than IO::File?  I use it all the time myself.

use Apache;
use Fcntl qw/:DEFAULT :flock/;
my $fh = Apache-gensym();
sysopen($fh, "file", O_RDONLY) || die "Can't open: $!";
flock($fh, LOCK_SH) || die "Can't lock: $!";
...

when $fh goes out of scope it's closed and unlocked.

Also see the guide section on exception handling.

-- 
Matt/

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org http://xml.sergeant.org




Re: passing Apache::File to XS code that expects FILE *?

2000-05-18 Thread Stas Bekman

On Thu, 18 May 2000, Vivek Khera wrote:

  "DM" == Doug MacEachern [EMAIL PROTECTED] writes:
 
 DM On Wed, 17 May 2000, Matt Sergeant wrote:
  Well, this may be true, but if you load IO::File before startup then it's
  not too big a deal...
 
 DM but it still adds a great deal of bloat to the server.  and it's oo
 DM interface, while very slick, adds quite a bit of runtime overhead, turn
 DM the sugar sour imo.
 
 In an embedded environment like mod_perl, then how do you suggest to
 deal with the dangling file handles problem?  That is, I'm processing
 a file or two, and some error occurs.  In a normal perl program, I'd
 exit or return out and then when the program terminates, it
 automagically closes all the files.  In mod_perl, the auto-close
 doesn't happen until much later.  With the OO interface, when the
 handle goes out of scope, such as a function call return, the file is
 implicitly closed.
 
 What other mechanism do you propose to handle this situation other
 than IO::File?  I use it all the time myself.

guide... guide... guide... :) (I'll keep you updated with the search
really soon now)

http://perl.apache.org/guide/porting.html#Filehandlers_and_locks_leakages

perl  5.6
use Symbol;
{ 
  my $fh = gensym;
  open $fh, 
}

perl5.6

{
  open my $fh, ...
}


_
Stas Bekman  JAm_pH --   Just Another mod_perl Hacker
http://stason.org/   mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://perl.org http://stason.org/TULARC
http://singlesheaven.com http://perlmonth.com http://sourcegarden.org




passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Jim Winstead

Is there some trick to passing an Apache::File to a function from
an XS module that expects a FILE *?

There's too much perl magic going on in the Apache::File implementation
for me to see where I can just pull the FILE * out.

(Its not strictly necessary that I do this, of course, it would just
be nice so I can use Apache::File-tmpfile(). Of course I can do the
same basic thing with POSIX::tmpnam().)

Jim



Re: passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Matt Sergeant

On Wed, 17 May 2000, Jim Winstead wrote:

 Is there some trick to passing an Apache::File to a function from
 an XS module that expects a FILE *?
 
 There's too much perl magic going on in the Apache::File implementation
 for me to see where I can just pull the FILE * out.
 
 (Its not strictly necessary that I do this, of course, it would just
 be nice so I can use Apache::File-tmpfile(). Of course I can do the
 same basic thing with POSIX::tmpnam().)

Or IO::File-new_tmpfile();

-- 
Matt/

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org http://xml.sergeant.org




Re: passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Jim Winstead

On May 17, Matt Sergeant wrote:
 Or IO::File-new_tmpfile();

I'd rather not go there.

http://marc.theaimsgroup.com/?l=apache-modperlm=95454378223412w=2

Jim



Re: passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Matt Sergeant

On Wed, 17 May 2000, Jim Winstead wrote:

 On May 17, Matt Sergeant wrote:
  Or IO::File-new_tmpfile();
 
 I'd rather not go there.
 
 http://marc.theaimsgroup.com/?l=apache-modperlm=95454378223412w=2

Well, this may be true, but if you load IO::File before startup then it's
not too big a deal...

Alternatively use File::Temp on CPAN, Apache-gensym(), and
open()/sysopen().

-- 
Matt/

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org http://xml.sergeant.org




Re: passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Doug MacEachern

On Wed, 17 May 2000, Jim Winstead wrote:

 Is there some trick to passing an Apache::File to a function from
 an XS module that expects a FILE *?

so long as the xsub uses a FILE *, the typemap will take care of the
magic.  for example, Apache::send_fd() is an xsub that uses the FILE *
typemap:

use Apache::File ();

my $r = shift;

$r-send_http_header;

my $tmp = Apache::File-tmpfile;

print $tmp "hi";

seek $tmp, 0, 0;

$r-send_fd($tmp);




Re: passing Apache::File to XS code that expects FILE *?

2000-05-17 Thread Doug MacEachern

On Wed, 17 May 2000, Matt Sergeant wrote:
 
 Well, this may be true, but if you load IO::File before startup then it's
 not too big a deal...

but it still adds a great deal of bloat to the server.  and it's oo
interface, while very slick, adds quite a bit of runtime overhead, turn
the sugar sour imo.