[HRPM] scaler or array and why?

chicks at chicks.net chicks at chicks.net
Wed Aug 23 12:31:27 CDT 2000


On Wed, 23 Aug 2000, jeff wrote:
> Michael Theiss wrote:
> > Question: I need to put all of parsed and interpolated output of
> > master_htm.pl  page into a variable that will then be assigned to an array.
> 
>  Well, I have a couple of (maybe helpful) comments:

I'm going to make a couple of nits about your helpful comments. ;-)  [ For
the uninitiated, Jeff and I maintain some friendly harassment about our
different styles of getting things accomplished in perl.  TIMTOWTDI. ]

> 0. Always 'use strict'. Always. Always always always.'-w' is a good
> idea too.

Always use '-w'.  'use strict' is usually a good idea.  :-)  If I'm
writing something I'm going to throw away after one use, use strict isn't
worth the trouble.  For anything mod_perl, serious applications
programming, anything with more than 3 functions, then always 'use
strict'.  Sometimes 'strict' is just too much of a pain.  When i was doing
lots of CGI programming and working with alternative name spaces for
variables the program just wouldn't work under a total use strict.

> 1. Anytime you find yourself writing the same code multiple times,
> it's a good bet that you can genericize it down to one. Why not make
> all of your PostIt() subs a single sub, and pass it arguments instead?
> Something like:
> 
>    From the calling code:
> 
> my $filename =
> "/home/alphaudt/www/cgi-bin/0/includes/onmouseover.shtml";
> &post_it($filename);

The ampersand (&) is only necessary for perl4 compatibility or creating
references and it's basically ugly for just a plain call.

>    The sub:
> 
> sub post_it{
>     my $filename = shift;
>     open(FILE, $filename) or die "can't open $filename: $!\n";
>     local $/ = undef;
>     my $whole_file = <POST>;
>     close(FILE) or die "Error closing $filename: $!\n";
>     print $whole_file;
> }

If you take the newline off the die it will give you the line number which
is usually pretty helpful.

>    Better yet, stuff the names of your files into an array and use that
> to call the sub:
> 
> my @files = qw( /home/alphaudt/www/cgi-bin/0/includes/onmouseover.shtml
> 		/home/alphaudt/www/cgi-bin/0/includes/meta_data.shtml
> 		you_get_the_idea );
> 
>     Then just:
> 
> &post_it( $files[0] );
> &post_it( $files[1] );

foreach $file (@files) { post_it($file); } # :-)

> 3. Use here documents for printing multiple lines. Instead of:
> 
> print "Content-type: text/html\n\n";
> print "<head>\n";
> print "<!--START META DATA -->\n";
> 
>  do this:
> 
> print <<END;
> Content-type: text/html
> 
> <head>
> <!--START META DATA -->
> END

You can also just let your data be a big quoted string:

print 'Content-type: text/html

<head>
<!--START META DATA -->
';

I used single quotes in this case and I recommend doing so by default.  If
you need interpolation you can switch to double quotes and escape all the 
bad characters.

-- 
</chris>

There are two ways of constructing a software design. One way is to make
it so simple that there are obviously no deficiencies. And the other way
is to make it so complicated that there are no obvious deficiencies.
                                                        - - C.A.R. Hoare




More information about the Norfolk-pm mailing list