Re: Apache::File correction

2002-04-13 Thread Dominic Mitchell

Rob Nagler [EMAIL PROTECTED] writes:

  undef $/;   # enable slurp mode
 
 I think the local is pretty important, especially in mod_perl:
 
 local $/;
 
 This has the same effect (the undef is unnecessary).  It's also a
 good idea to enclose the code in a subroutine with error checking:
 
 sub read_file {
 my($file) = @_;
   open(FH,  $file) || die(error opening $file: $!);
 local($/);
   my($content) = FH;
   close(FH)  defined($content) || die(error reading $file: $!);
   return \$content;
 }

A similiar idiom that I saw recently:

sub file_contents {
my $fn = shift;
open(FOO, $fn)
or die file_contents: open($fn): $!\n;
my $stuff;
read FOO, $stuff, -s FOO;
close(FOO);
return $stuff;
}

Take your pick as to which one is clearer...

-Dom

-- 
| Semantico: creators of major online resources  |
|   URL: http://www.semantico.com/   |
|   Tel: +44 (1273) 72   |
|   Address: 33 Bond St., Brighton, Sussex, BN1 1RD, UK. |



Re: Apache::File correction

2002-04-12 Thread Ernest Lergon

Martin Haase-Thomas wrote:
 
 [snip] Secondly I wonder whether local $/ = undef
 will have any effect. But I've never tried overriding Perl's predefined
 variables.
 
 regards

Dear Martin,

this is the well-known file-slurp mode.

E.g.:

undef $/;   # enable slurp mode
$_ = FH;  # whole file now here
s/\n[ \t]+/ /g;


Look for slurp in your perl docs.

Ernest




-- 

*
* VIRTUALITAS Inc.   *  *
**  *
* European Consultant Office *  http://www.virtualitas.net  *
* Internationales Handelszentrum *   contact:Ernest Lergon  *
* Friedrichstraße 95 *mailto:[EMAIL PROTECTED] *
* 10117 Berlin / Germany *   ums:+49180528132130266 *
*
   PGP-Key http://www.virtualitas.net/Ernest_Lergon.asc




Re: Apache::File correction

2002-04-12 Thread Rob Nagler

 undef $/;   # enable slurp mode

I think the local is pretty important, especially in mod_perl:

local $/;

This has the same effect (the undef is unnecessary).  It's also a
good idea to enclose the code in a subroutine with error checking:

sub read_file {
my($file) = _;
open(FH,  $file) || die(error opening $file: $!);
local($/);
my($content) = FH;
close(FH)  defined($content) || die(error reading $file: $!);
return \$content;
}

Rob





Apache::File correction

2002-04-10 Thread Rasoul Hajikhani

Folks,
The Apache::File man pages indicate that 

($name,$fh) = Apache::File-tmpfile;

returns a fh ready to write to. So far so good. 

In case of wanting to read from it, here is what I do:

# Is this necessary?
$fh-close() or die Could not close $name: $!\n;

$fh-open($name);
local $/= undef;
$output = $fh;
warn $output\n; 
$fh-close() or die Could not close $name: $!\n;

But $output is empty on each request. Is there an error somewhere that I
am not seeing? I appreciate all comments.responses.
Thanks in advance
-r



Re: Apache::File correction

2002-04-10 Thread Robert Landrum

At 1:44 PM -0700 4/10/02, Rasoul Hajikhani wrote:
Folks,
The Apache::File man pages indicate that

($name,$fh) = Apache::File-tmpfile;

returns a fh ready to write to. So far so good.

In case of wanting to read from it, here is what I do:

# Is this necessary?
$fh-close() or die Could not close $name: $!\n;

$fh-open($name);
local $/= undef;
$output = $fh;
warn $output\n;
$fh-close() or die Could not close $name: $!\n;


To me it appears that you have not written anything to your tmp 
file... Which would explain the empty $output.

Usually temp file creators open in read/write mode.

So if you say something like

($name,$fh) = Apache::File-tmpfile;

print $fh Hello World;

seek($fh,0,0);

my $line = $fh;

print $line;

It should print Hello World;

Rob



--
When I used a Mac, they laughed because I had no command prompt. When 
I used Linux, they laughed because I had no GUI.  



Re: Apache::File correction

2002-04-10 Thread Rasoul Hajikhani

Robert Landrum wrote:
 
 At 1:44 PM -0700 4/10/02, Rasoul Hajikhani wrote:
 Folks,
 The Apache::File man pages indicate that
 
 ($name,$fh) = Apache::File-tmpfile;
 
 returns a fh ready to write to. So far so good.
 
 In case of wanting to read from it, here is what I do:
 
 # Is this necessary?
 $fh-close() or die Could not close $name: $!\n;
 
 $fh-open($name);
 local $/= undef;
 $output = $fh;
 warn $output\n;
 $fh-close() or die Could not close $name: $!\n;
 
 To me it appears that you have not written anything to your tmp
 file... Which would explain the empty $output.


gpg writes to that file.
 
 Usually temp file creators open in read/write mode.
 
 So if you say something like
 
 ($name,$fh) = Apache::File-tmpfile;
 
 print $fh Hello World;
 
 seek($fh,0,0);
 
 my $line = $fh;
 
 print $line;
 
 It should print Hello World;
 
 Rob
 
 --
 When I used a Mac, they laughed because I had no command prompt. When
 I used Linux, they laughed because I had no GUI.