Skip to content

Instantly share code, notes, and snippets.

@exodist
Created July 28, 2014 21:45
Show Gist options
  • Save exodist/5a3bee9266348facbe5f to your computer and use it in GitHub Desktop.
Save exodist/5a3bee9266348facbe5f to your computer and use it in GitHub Desktop.
Bad idea, thanks Josh
package Test::Calculate;
use strict;
use warnings;
use Data::Dumper;
use base 'Exporter';
use Carp qw/croak/;
our @EXPORT = ('CALCULATE');
sub CALCULATE {
my ($struct) = @_;
local $Data::Dumper::Purity = 1;
local $Data::Dumper::Useqq = 1;
local $Data::Dumper::Deepcopy = 1;
local $Data::Dumper::Sortkeys = 1;
my $data = Dumper($struct);
$data =~ s/\$VAR1 = //;
$data =~ s/;$//;
chomp($data);
my @caller = caller();
my $file = $caller[1];
open(my $fh, '<', $file) || die "Could not open $file: $!";
my $updated = 0;
my @lines;
while (my $line = <$fh>) {
if ($line =~ m/CALCULATE/) {
croak "CALCULATE must be called with parentheses, and must be given a scalar variable for an arg."
unless $line =~ m/CALCULATE\(\s*\$[0-9A-Za-z_]+\s*\)/;
$line =~ s/CALCULATE\(\s*\$[0-9A-Za-z_]+\s*\)/$data/;
}
push @lines => $line;
}
close($fh);
open($fh, '>', $file) || die "Cannot open $file for writing: $!";
print $fh @lines;
return $struct;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment