Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Created September 29, 2019 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/56858a0e2f50dce42399a9680dc583ed to your computer and use it in GitHub Desktop.
Save adamcrussell/56858a0e2f50dce42399a9680dc583ed to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 027
package Audit{
use Data::Dump q/pp/;
use Filter::Simple;
our %Log;
sub audit{
my($name, $value) = @_;
$value = "undef" if !defined($value);
if($Log{$name}){
push @{$Log{$name}}, $value;
}
else{
$Log{$name} = [$value];
}
}
sub clear_log{
%Log = ();
}
sub print_log{
for my $key (keys %Log){
print "$key: " . join(", ", @{$Log{$key}}) . "\n";
}
}
sub pretty_print_log{
for my $key (keys %Log){
print "$key: " . join(", ", map{ pp $_ } @{$Log{$key}}) . "\n";
}
}
FILTER_ONLY code => sub{
s|((\$[[:alnum:]_]*).*;)|$1 Audit::audit('$2', $2);|g;
}
}
use strict;
use warnings;
##
# Write a script to find the intersection of two straight lines.
# The co-ordinates of the two lines should be provided as command line parameters.
##
MAIN:{
my @endpoints_line_0 = @ARGV[0, 1];
my @endpoints_line_1 = @ARGV[2, 3];
@endpoints_line_0 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_0;
@endpoints_line_1 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_1;
my($a, $b, $c, $d, $p, $q, $r, $s) = (
$endpoints_line_0[0]->[0],
$endpoints_line_0[0]->[1],
$endpoints_line_0[1]->[0],
$endpoints_line_0[1]->[1],
$endpoints_line_1[0]->[0],
$endpoints_line_1[0]->[1],
$endpoints_line_1[1]->[0],
$endpoints_line_1[1]->[1]
);
my $denominator = ($c - $a) * ($s - $q) - ($r - $p) * ($c - $b);
my $x0 = ($c * $b - $a * $d) * ($r - $p) - ($r * $q - $p * $s) * ($c - $a);
my $y0 = ($c * $b - $a * $d) * ($s - $q) - ($r * $q - $p * $s) * ($d - $b);
my $x = $x0/$denominator;
my $y = $y0/$denominator;
print "($a, $b)--($c, $d) and ($p, $q)--($r, $s) intersect at ($x, $y)\n";
}
use strict;
use warnings;
##
# Write a script that allows you to capture/display historical data.
# It could be an object or a scalar.
##
use Audit;
use Class::Hash;
my $test0;
$test0 = 2;
$test0 = 3;
Audit::print_log();
Audit::clear_log();
my $test1 = new Class::Hash(A => "B");
$test1 = new Class::Hash(B => "C");
$test1 = new Class::Hash(C => "D");
Audit::pretty_print_log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment