[H-GEN] A perl query

Jason Parker-Burlingham jasonp at uq.net.au
Sat May 3 01:20:27 EDT 2003


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Three Blokes <gerbil at bigpond.net.au> writes:

> So far, so good, it's spitting out the statistics to screen, and my
> html is made, iv even got perl to write the statistics to the html,
> But all good things must end, and so my problem: I have been reading
> the source html line by line(e.g $lineofhtml = <HTMLFILEHANDLE>)
> doing a search and replace to insert the data coming from my
> logfile, and then writing to the output directory with print
> $lineofhtml;

This scheme can work well if you're careful to ensure the HTML coming
is unambiguous; if you're writing it yourself that'll be good.  You
*might* want to look at the Template Toolkit when you're more
confident with Perl.

> which works fine, except for the fact that it misses some lines!!,

Now, this is odd.  Since you've posted no code it's hard to say what's
going on but I would assume that the basics are like this[1]:

	while(defined(my $input = <HTMLFH>)) {
	        s/SPECIALTAG/$logfile_data/;
	        print;
	}

> now i'm assuming that it's skipping lines because they have Special
> Characters in them(although it skips a line that just has <head>
> written on it, which as far as I can see doesnt have any special
> characters on it?)

In general there are no `special characters' that will make Perl fail
to read or write a line of input.

What *may* be happening is that your search for a portion of the input
to replace is accidentally matching these other lines and messing with
it.  Again, without seeing some code and sample input it's very hard
to say for certain.

> so my question to anyone who knows perl is this:  I could use read()
> to read the entire file into a scalar, then search and replace it,
> then write() it back to the output, is this a good idea?

Yes, and maybe.  If the input file is extremely large it will be very
inefficient to do so, but if it's small, you don't have to worry a
great deal:

	local $/ = undef;
	my $html = <STDIN>;
	# make changes to $html;
	print $html;

> , is there a limitation on the size of a scalar that could cause
> problems when my output reaches a certain size?

Perhaps, though I can't think of what that value would be in general.
It's probably system-dependent or limited by available memory.

> , and finally, will this overcome the special character problem im
> having currently?

Almost certainly not.  In fact, since $html would then have embedded
newlines, it would actually make your job a little harder since you
will have to learn about the /s and /m regex modifiers.

> , in everything i'v read so far they say that read() will read a
> certain amount from a file, but doesnt specify whether the data will
> contain special characters or not?

The only character I can think off offhand that will present any
problems when reading from a filehandle (I wouldn't recommend using
Perl's C<read> or C<sysread> functions for this program) would be a
null (ASCII 0), but a quick check on my own system indicates even that
presents no problem.

> Any help will be greatly appreciated, and of course i'm completely
> open to any other ways people can think of that I could do this,
> along with criticism and/or flames.

This is how I would write it.

   #!/usr/bin/perl -w
   
   use strict;
   use warnings;
   
   print <<EOHEADER;
   <html>
           <head>
                   <title>Example Page</title>
           </head>
           <body>
                   <table>
                           <tr><th>headings</th><th>etc.</th></tr>
   EOHEADER
   
   # calculate your logfile stats, and print them as HTML using print
   # statements like this, in a while() loop or similar.
   print "<td>$result</td>";
   
   # now print the end of the HTML by closing tags, etc.
   print <<EOFOOTER;
                   </table>
           </body>
   </html>
   EOFOOTER

This is all totally untested and pretty half-assed.

For help with your real problem, reply to this message (taking care,
of course, to quote it correctly) with more details about your script
(the guts of the loop handling I/O and a small amount of sample
input would do) and I'll see what I can do.

Cheers,

jason

[1] : Perl's -p option puts a while() loop a lot like this around the
      code supplied as the script, making it very easy to write a
      program like this as a one-liner.
-- 
``Oooh!  A gingerbread house!  Hansel and Gretel are set for life!''

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list