Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntactic Sugar and speed 1

Status
Not open for further replies.

dwhalen

Programmer
Feb 22, 2002
105
CA
Do the various 'Syntactic Sugar' options slow down perl programs?

ex:

Code:
#$r is an apache request object
$r->print("<form name=\"thisform\"");

#vs.

$r->print(qq@
          <form name="thisform"
           @);


I am just wondering because I find the qq makes complicated html print statements easier to debug....you know trying to find that stray "

Thanks.

Later
 
Why are you using the Apache Request to print..

You should use it like the example below.

Code:
# Print the HTTP header
$r->status(200);
$r->content_type("text/html; charset=ISO-8859-1");
$r->header_out('Expires' => "Thu, 06 Jan 1973 00:00:00 PST");
$r->header_out('Cache-Control' => "private");

# Then output your HTML....
print qq | My HTML GOES HERE |;


M. Brooks
X Concepts LLC
 
Or in OO you can do create a class like this..
Code:
sub new {
        my $class = shift;
        my $self = {};

        $class = ref($class) || $class;
        bless $self, $class;

        $self->init_apr; # Init the Apache::Request

        return $self;
}

sub init_apr {
        my $self = shift;

        require Apache::Request;

        # Use Apache::Request object
        $self->{APR} = Apache::Request->new($self->{APR},
                DISABLE_UPLOADS =>      1,
        );
}

sub print_http_header {
        my $self = shift;
                 
        $self->{APR}->status(200);
        $self->{APR}->content_type("text/html; charset=ISO-8859-1");
        $self->{APR}->header_out('Expires' => "Thu, 06 Jan 1973 00:00:00 PST");
        $self->{APR}->header_out('Cache-Control' => "private");
}

and use it like...
Code:
use Module;
my $r = Module->new;
$r->print_http_header;

print qq | My HTML GOES HERE |;

M. Brooks
X Concepts LLC
 
There's many benchmarking modules available for you to test and see, but I doubt there's any speed difference. Maybe one takes longer to parse than the other, I don't know. Even if that's the case, it's miniscule and only on startup (and if you're concerned about speed, you're already using a code caching scheme like mod_perl, right?).

I benchmarked the print function awhile ago to see whether it's faster to concatenate, interpolate, or list arguements. While there were occasionally differences, I don't think they were consistent enough or drastic enough to be any concern. Use what's easy and maintainable, then if you have performance issues, profile the application, and optimize the bottlenecks (most often it's your database, learn stored procedures).

________________________________________
Andrew - Perl Monkey
 
Thanks for your input.

Yeah, I didn't think there was any difference in speed or if any very miniscule. However, I like to program that way because I feel it is eaiser to read and debug, whereas some do not and I get the feeling that they feel it is bad programming or something....like doing a print with one big runon statement of html is better programming then spacing things out and using the qq to make it more readable. I don't know, I just didn't want to be doing something that was slowing down my programs.


Later
 
That's probably about the speediest way for perl to generate html, but the most maintainable way it to completely separate your code from your markup. While there are many html templating solutions, my personal favorite is HTML::Template. It's certainly not as fast, but more than makes up for it in maintainability (something I'm trying to convince the guys at work of).

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top