#!/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( scope => { 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;
}
####
$ ./pi2.pl
Died at ./pi2.pl line 36.
$
##
##
#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
##
##
$ 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.pm':
>> 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{ns}, $sub );
283: die $@ if $@; # be nice and die for easier debuggering
284 }
285 }
286: return $opts{scope};
287 }
288
>>
>> s
Sub::Genius::run_once((eval 44)[/usr/local/share/perl/5.30.0/Sub/Genius.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 debuggering
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", HASH(0x564d07e0f210)) called at /usr/local/share/perl/5.30.0/Sub/Genius.pm line 245
Sub::Genius::run_any(Sub::Genius=HASH(0x564d07b619b8), "scope", HASH(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>
##
##
$ ./pi3.pl
pi = 3.769908
$
##
##
#!/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 = ;
chomp $prompt;
die unless ( $prompt eq "y" );
## special do-hickeys I want at the end
my @list = ( '[id://1177642]', '[mod://Astro::Coord::ECI::Sun]', '[|]', '[|]', '[|]','<', '>', '[', ']' ,'');
@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 = '';
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 = ;
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__
##
##
use warnings;
use 5.011;