Gossamer Forum
Home : General : Perl Programming :

A better way?

(Page 1 of 2)
> >
Quote Reply
A better way?
Do you think there's a better, faster way of doing this?

Code:
use strict;

use CGI;
my $query = CGI->new();

print $query->header();

my @allhtml = ('index.apricot.html',
'index.butter.html',
'index.garden.html',
'index.greek.html',
'index.greekhoney.html',
'index.maple.html',
'index.natural.html',
'index.raspberry.html',
'index.strawberry.html',
'index.lemons.html',
'index.rhubarb.html',
'index.vanilla.html'
);

my $randhtml = $allhtml[int rand @allhtml];

my $HTML;

{ local $/;
open (FILE,"</home/fba/rachelsorganic.co.uk/_html/index/$randhtml") or die $!;
$HTML = <FILE>;
close FILE;
}

print $HTML;

- wil
Post deleted by Paul In reply to

Last edited by:

Paul: Apr 11, 2002, 9:49 AM
Quote Reply
Re: [Wil] A better way? In reply to
Ok here we go:

Code:
use strict;
use CGI qw/:standard/;

my $allhtml = [qw/butter garden greek greekhoney maple natural raspberry strawberry lemons rhubarb vanilla/];
my $path = "/home/fba/rachelsorganic.co.uk/_html/index/index.";

open (FILE, ($path . $allhtml->[rand @$allhtml] . ".html")) or die $!;
print while <FILE>;
close FILE;

Last edited by:

Paul: Apr 11, 2002, 9:54 AM
Quote Reply
Re: [Paul] A better way? In reply to
Hm. I've been fiddling around with this a lot. I think I've come up with a more flexible solution, and hopefully faster as it doesn't load the file into memory. What do you think of this?

Code:
my @files = glob '/home/fba/rachelsorganic.co.uk/_html/index/index.*.html';

{ local $/;
open FILE, '<' . $files[rand @files] or die $!;
print while <FILE>;
close FILE;
}

_ untested _

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Im not sure its any faster as perl has to load a seperate program.

Last edited by:

Paul: Apr 12, 2002, 1:52 AM
Quote Reply
Re: [Paul] A better way? In reply to
I guess I should benchmark these.

I think it's more of a flexible solution as people can add more files to the directory without altering any code.

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Although a couple more lines of code I think readdir is quicker than glob.
Quote Reply
Re: [Paul] A better way? In reply to
Yeah, you're probably right. readdir is a perl function where glob is a seperate program, right?

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Should I be setting a buffer here of say:

local $/ = \8192;

So that it reads in the file chunk by chunk?

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Why do you want to increase the buffer?

You only really need 8192 when reading multipart.
Quote Reply
Re: [Paul] A better way? In reply to
To break down the file into more managable chunks? Supposedly this speeds things up?

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Quote:
local $/ = \8192;

Rather then using special variables, make your code more obvious by doing:

while (read(FILE, $buffer, 8192)) {
print $buffer;
}

much easier to understand, espcially if you don't know what $/ does. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Paul] A better way? In reply to
Paul,

I noticed that you are really fond of using reference as I refer to your previous reply..

Is it speedy in maniplating data?

Wil,

What is the result of your benchmark test..
Quote Reply
Re: [golden_water] A better way? In reply to
References are just more flexible for storing data.
Quote Reply
Re: [Paul] A better way? In reply to
for complex data storing...sure for its flexibility

however, for simple one.....how about the overhead...

Well, I would like to know wil's test first and see how difference is it...
Quote Reply
Re: [golden_water] A better way? In reply to
...sure, its not needed for that simple example above but I just felt like using a reference, so I did :)

Last edited by:

Paul: Apr 13, 2002, 5:50 AM
Quote Reply
Re: [Alex] A better way? In reply to
What about something like:

Code:
open(FILE, ... ) or die $!:
print while sysread(FILE, $_, 8192);
close FILE;

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Read paragraph 1:

http://www.perldoc.com/...od/func/sysread.html
Quote Reply
Re: [Paul] A better way? In reply to
Yes... and? Am I missing something?

- wil
Quote Reply
Re: [Wil] A better way? In reply to
As you asked a question about the code I assumed you wanted to know if it was right ;) ...hence the sysread perldoc page :)

Last edited by:

Paul: Apr 14, 2002, 8:01 AM
Quote Reply
Re: [Paul] A better way? In reply to
Hm. I knew my code was right.. just wondering if it was good and better than what I had :-)

- wil
Quote Reply
Re: [Wil] A better way? In reply to
Mmm what made you want to change to sysread anyway?

If it was any better I guess Alex would have used it in the first place Laugh

Last edited by:

Paul: Apr 14, 2002, 8:06 AM
Quote Reply
Re: [Paul] A better way? In reply to
I dunno. Just a hunch.

After all, TMTOWTDI.

- wil
Quote Reply
Re: [Wil] A better way? In reply to
There is MTOWTDI sometimes and when you have good reason to change it, but not just on a hunch :)

Hmm here's my hunch....

I think you would really want to put this in your code just inside the while loop...

sleep(time());

Cool
Quote Reply
Re: [Wil] A better way? In reply to
I think what Paul is trying to say (very cryptically), is you shouldn't mix sysread with open. It should only be used on files you sysopen (due to binmode and blocking issues).

You can do:

print while (read(FILE, $_, 8192));

which is equivalent to what I did, but shorter.

Cheers,

Alex
--
Gossamer Threads Inc.
> >