Skip to content

Instantly share code, notes, and snippets.

@davorg
Last active May 20, 2016 13:09
Show Gist options
  • Save davorg/207dee895ee44e84d91a230a516000f5 to your computer and use it in GitHub Desktop.
Save davorg/207dee895ee44e84d91a230a516000f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/:standard/;
print header;
##
## property detail - shows a single property
##
my $ier_base = 'http://www.ithacaestatesrealty.com';
my $class_file = param('class');
my $record_id = param('key');
die "Insufficient data\n" unless $class_file && $record_id;
my $property = get_property($class_file, $record_id);
my $output = process_template('toptpl.tpl', $property) .
process_template('singledetail.tpl', $property) .
process_template('bottomtpl.tpl', $property);
print $output;
sub get_property {
my ($file, $id) = @_;
my @columns = qw[RId RName RAddress RType RRooms RNeighborhood RSQft RPrice
RDescription RImage1 RImage2 RImage3 RImage4 RFloorplan
RBaths RImage5 RImage6 RImage7 RUtils
RSchool RStatus];
open my $fh, '<', "$file.cls" or die $!;
my %prop;
while (<$fh>) {
next unless /^$id\^/;
chomp;
@prop{@columns} = split /\^/;
last;
}
if (!%prop) {
die "Property $id not found in class $file\n";
}
return \%prop;
}
# Based on an example in perlfaq4.
# But really - use a real templating engine!
sub process_template {
my ($template, $data) = @_;
open my $tmpl_fh, '<', $template or die;
my $processed;
while (<$tmpl_fh>) {
s/<% (\w+?) %>/$data->{$1}/g;
$processed .= $_;
}
return $processed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment