Hi Brett,
Welcome to the monastery. I hope your friendly presence makes it feel less cloistered here. I've taken the opportunity to look and listen to your presentations, and I'm so glad that you took the time you needed to get through the initial arc of it. I've been in conversations with people who do representations that take 1, 3, 6 hours, and you seem to be of this variety, ideas coming out of your ideas like the florets of a cauliflower("...and in the infinite case....") You should know that I'm only intermediate as a practioner of perl, despite having been at it for a while now.
I have some pretty grandiose ideas of where this software could take us, but it would have to be an "us" thing. By that I mean, you, me, and others would have to get their paws on this if it's gonna realize its goal. One might think that there's fewer perl practioners to help with anything, but what if it offered the carrot of not having bad forks nor race conditions, provided that the user doesn't embellish and add them? The last perl script I wrote with fork was equal amounts hideous and faulty.
I've worked through several examples. The five older ones are solid, but the last 3 pi ones need some more spitshine. My theory is that you wrote over your previous work. Let's take a look at the second one first. I will list source, output from stdout, source text from debugger, then debugger output:
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'state';
use Sub::Genius;
my $NUM_THREADS = 5;
my $preplan = q{
(
step0
&
step1
&
step2
&
step3
&
step4
&
step5
)
reduce
};
my $final_scope = Sub::Genius->new( preplan => $preplan )->run_any( sc
+ope => { sum => 0.0, num_steps => 1_000_000, pi => undef } );
printf qq{pi = %f\n}, $final_scope->{pi};
sub AUTOLOAD {
our $AUTOLOAD;
my $sub = $AUTOLOAD;
$sub =~ s/.*:://;
die if $sub !~ m/^step([\d]+)/;
my $step_id = $1;
# deal with 'step'
my $old_scope = shift;
my $new_scope = _snep( $step_id, $old_scope );
return $new_scope;
}
sub _snep {
my ( $step_id, $scope ) = @_;
$scope->{sum} = _do_calc( $step_id, $scope );
return $scope;
}
sub reduce {
my $scope = shift;
my $num_steps = $scope->{num_steps};
my $step = 1 / $num_steps;
$scope->{pi} = $scope->{sum} * $step;
return $scope;
}
sub _do_calc {
my ( $id, $scope ) = @_;
my $sum = $scope->{sum};
my $num_steps = $scope->{num_steps};
my $step = 1 / $num_steps;
for ( my $i = $id ; $i < $num_steps ; $i += $NUM_THREADS ) {
my $x = ( $i + 0.5 ) * $step;
$sum += 4.0 / ( 1 + $x * $x );
}
return $sum;
}
Stdout has:
$ ./pi2.pl
Died at ./pi2.pl line 36.
$
This is my debugger source text, 3.scope.txt:
#source 2.scope.txt
f Genius.pm
b 282
s
n
n
p $sub
v
s
v
s
v
b 35
c
n
p $sub
p $sub !~ m/^step([\d]+)/
#save 3.scope.txt
Debug output:
$ perl -d pi2.pl
Loading DB routines from perl5db.pl version 1.55
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(pi2.pl:9): my $NUM_THREADS = 5;
DB<1> source 3.scope.txt
>> #source 2.scope.txt
>> f Genius.pm
Choosing /usr/local/share/perl/5.30.0/Sub/Genius.pm matching 'Genius.p
+m':
>> b 282
>> s
main::(pi2.pl:11): my $preplan = q{
main::(pi2.pl:12): (
main::(pi2.pl:13): step0
main::(pi2.pl:14): &
main::(pi2.pl:15): step1
main::(pi2.pl:16): &
main::(pi2.pl:17): step2
main::(pi2.pl:18): &
main::(pi2.pl:19): step3
main::(pi2.pl:20): &
main::(pi2.pl:21): step4
main::(pi2.pl:22): &
main::(pi2.pl:23): step5
main::(pi2.pl:24): )
main::(pi2.pl:25): reduce
>> n
main::(pi2.pl:28): my $final_scope = Sub::Genius->new( preplan => $
+preplan )->run_any( scope => { sum => 0.0, num_steps => 1_000_000, pi
+ => undef } );
>> n
Sub::Genius::run_once(/usr/local/share/perl/5.30.0/Sub/Genius.pm:282):
282: eval sprintf( qq{%s%s(\$opts{scope});}, $opts{ns},
+ $sub );
>> p $sub
step
>> v
279 # main run loop - run once
280: local $@;
281: foreach my $sub (@seq) {
282==>b eval sprintf( qq{%s%s(\$opts{scope});}, $opts{n
+s}, $sub );
283: die $@ if $@; # be nice and die for easier debu
+ggering
284 }
285 }
286: return $opts{scope};
287 }
288
>>
>> s
Sub::Genius::run_once((eval 44)[/usr/local/share/perl/5.30.0/Sub/Geniu
+s.pm:282]:1):
1: main::step($opts{scope});
>> v
1==> main::step($opts{scope});
2 ;
>> s
main::AUTOLOAD(pi2.pl:33): our $AUTOLOAD;
>> v
30: printf qq{pi = %f\n}, $final_scope->{pi};
31
32 sub AUTOLOAD {
33==> our $AUTOLOAD;
34: my $sub = $AUTOLOAD;
35: $sub =~ s/.*:://;
36: die if $sub !~ m/^step([\d]+)/;
37: my $step_id = $1;
38
39 # deal with 'step'
>> b 35
>> c
main::AUTOLOAD(pi2.pl:35): $sub =~ s/.*:://;
>> n
main::AUTOLOAD(pi2.pl:36): die if $sub !~ m/^step([\d]+)/;
>> p $sub
step
>> p $sub !~ m/^step([\d]+)/
1
>> #save 3.scope.txt
DB<10> s
+
Sub::Genius::run_once(/usr/local/share/perl/5.30.0/Sub/Genius.pm:283):
283: die $@ if $@; # be nice and die for easier debu
+ggering
DB<10> s
+
Died at pi2.pl line 36.
at /usr/local/share/perl/5.30.0/Sub/Genius.pm line 283.
Sub::Genius::run_once(Sub::Genius=HASH(0x564d07b619b8), "scope", H
+ASH(0x564d07e0f210)) called at /usr/local/share/perl/5.30.0/Sub/Geniu
+s.pm line 245
Sub::Genius::run_any(Sub::Genius=HASH(0x564d07b619b8), "scope", HA
+SH(0x564d07e0f210)) called at pi2.pl line 28
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
DB<10>
The truth is that I don't get what goes wrong here, but I think I've framed where it happens. The scoping is hard for me to get my head around, but is it not the implementation of an AST?
I could take you through the first one in a similar way, but again, I think it got overwritten, so fix 2 then fix 1 makes sense in such a scenario to me. The third is tantalizing, but we seem to have some inflation there:
$ ./pi3.pl
pi = 3.769908
$
Have you read Borwein and Borwein re pi calculations? (A classic.) Worth the interlibrary loan if you haven't.
Anyways, I'm gonna get this posted hoping that the surfeit of brilliant perl programmers who suddenly have more time on the their hands than usual can pick up where I tap out.
Also, I've seen how you attack a keyboard, and monastery markup might be tedious from scratch. This is what I use as a template for posts:
#!/usr/bin/perl -w
use v5.030;
use Path::Tiny;
use POSIX qw(strftime);
# initialization that must precede main data structure
# User: enter a subdirectory you would like to create
# enter a subdirectory of this^^^ for output
my $ts = "perlmonks";
my $output = "writeups";
## turning things to Path::Tiny
my $abs = path(__FILE__)->absolute;
my $path1 = Path::Tiny->cwd;
my $path2 = path( $path1, $ts );
say "abs is $abs";
say "path1 is $path1";
say "path2 is $path2";
print "This script will build the above path2. Proceed? (y|n)";
my $prompt = <STDIN>;
chomp $prompt;
die unless ( $prompt eq "y" );
## special do-hickeys I want at the end
my @list = ( '[id://1177642]', '[mod://Astro::Coord::ECI::Sun]', '[|]
+', '[|]', '[|]','<', '>', '[', ']' ,'<ul>
<li>vanilla</li>
<li>chocolate</li>
</ul>');
@list = map { "$_\n" } @list;
say "list is @list";
my $return1 = write_monk_tags();
say "return1 is $return1";
my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime );
$munge .= ".monk.txt";
# use Path::Tiny to create and write to a text in relevant directory
my $save_file = path( $path2, $output, $munge )->touchpath;
my $return2 = $save_file->spew($return1);
say "return2 is $return2";
my $return3 = $save_file->append(@list);
say "return3 is $return3";
say "created file $save_file";
system( "gedit $save_file &");
sub write_monk_tags {
my $tag_pair = '<x></x>';
my $return = '';
# User: change these quoted values for different order or tags
my @buchstaben = qw/i p c pre readmore b/;
for my $letter (@buchstaben) {
print "How many $letter tag pairs would you like?: ";
my $prompt = <STDIN>;
chomp $prompt;
while ( $prompt gt 0 ) {
my $new_string = $tag_pair;
$new_string =~ s/x/${letter}/g;
say "new string is $new_string";
$return = $return . $new_string . "\n";
--$prompt;
}
}
return $return;
}
__END__
Edit: Removed the use of an earlier version and redundant use warnings in a subroutine.
use warnings;
use 5.011;
Have a great day. I like scripting in the AC when the alternative is an unforgiving sun....
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.