E
Eric
Hello,
I have a Perl application that I am working on optimizing. One thing I
am trying to do is implement subroutine calls by reference when
possible. However, after reading perlref, perlreftut, perlsub, and lots
more, I still am a unclear with how to proceed.
Here is an example of things I am trying to do...some suggestions on
where to implement references would greatly appreciated.
-------------------------------------------------------------------------------
use strict;
use warnings;
# Open the specified file and return the contents
sub open_template($) {
my $file = shift;
open(TMP, $file) || die("cannot open $file: $!");
my @code = <TMP>;
close(TMP);
return @code;
}
# Check the template for expected values
sub check_template(@) {
my @code = @_;
my $valid;
my $value = "value to find";
foreach my $line (@code) {
# check for match in $line
$valid = 1 if /$value/;
}
die("value not found in template") if !$valid;
}
# Replace tags with content
sub parse_template(@) {
my @code = @_;
my $html;
foreach (@code) {
# check for match in $line
# ex: s/<myCustomTag>/$myCustomValue/g;
$html .= "$_\n";
}
return $html;
}
# Path to the template
my $template = "path to file";
# Get the contents of the template
my @html = open_template($template);
# Validate template
check_template(@html);
# Print the parsed template
print parse_template(@html);
-------------------------------------------------------------------------------
It would seem to me that I would want open_template to return a
reference to @code. Then I would pass that reference to
check_template(). Then that reference would be passed to
parse_template(). Does that sound right? It seems like I am currently
creating multiple copies of the same data. Anyway, enough
rambling...comments and code suggestions welcomed =)
Thanks,
Eric
I have a Perl application that I am working on optimizing. One thing I
am trying to do is implement subroutine calls by reference when
possible. However, after reading perlref, perlreftut, perlsub, and lots
more, I still am a unclear with how to proceed.
Here is an example of things I am trying to do...some suggestions on
where to implement references would greatly appreciated.
-------------------------------------------------------------------------------
use strict;
use warnings;
# Open the specified file and return the contents
sub open_template($) {
my $file = shift;
open(TMP, $file) || die("cannot open $file: $!");
my @code = <TMP>;
close(TMP);
return @code;
}
# Check the template for expected values
sub check_template(@) {
my @code = @_;
my $valid;
my $value = "value to find";
foreach my $line (@code) {
# check for match in $line
$valid = 1 if /$value/;
}
die("value not found in template") if !$valid;
}
# Replace tags with content
sub parse_template(@) {
my @code = @_;
my $html;
foreach (@code) {
# check for match in $line
# ex: s/<myCustomTag>/$myCustomValue/g;
$html .= "$_\n";
}
return $html;
}
# Path to the template
my $template = "path to file";
# Get the contents of the template
my @html = open_template($template);
# Validate template
check_template(@html);
# Print the parsed template
print parse_template(@html);
-------------------------------------------------------------------------------
It would seem to me that I would want open_template to return a
reference to @code. Then I would pass that reference to
check_template(). Then that reference would be passed to
parse_template(). Does that sound right? It seems like I am currently
creating multiple copies of the same data. Anyway, enough
rambling...comments and code suggestions welcomed =)
Thanks,
Eric