Post by Malcolm McLeanPost by fred flintstonePost by Malcolm McLeanfred flintstone speaks English.
I've also faced similar problems, and used similar words. Ben was possibly misled
because "capture" is a term used with a specific meaning for lambda functions
and closures.
I didn't mean this, but now that you brought it up, is there a standard
implementation of closures?
When declaring a function that accepts a function pointer in C, it is good
practice to always add a void * as a parameter, and accept a void * to pass
back. Usually this is called something like the "context" pointer. If caller
doesn't need it, it is simply null. But it can point to a structure containing
detailed information, and it means you don't need a global (which can be almost
unworkable if you've got more than one instance of the function pointer live).
The context pointer is effectively a closure, but there's no real syntactical
support, and it won't automatically call a destructor when the function pointer
is destroyed.
Thanks for your elaboration, malcolm, it prompted the literal solution
of this thread.
Make query number 11? [Y] :Y
to server is 11 2457987.25976179 2457987.25883747 2457987.2592592
right ascension of moon 10h 4m 0s
right ascension of sun 10h 4m 0s
moon seconds is 36240
sun seconds is 36240
equal, while condition fails at julian day 2457987.2592592
This program was called with "./jup".
Value from perl : 2457987.259259
Can't use an undefined value as a SCALAR reference at 3.neptune.pl line 102.
$
----
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
double d;
printf ("This program was called with \"%s\".\n",argv[0]);
if (argc == 2)
{
d = atof(argv[1]);
printf("Value from perl : %f\n", d );
char array[20] = "Hello from C";
return ( array );
}
else
{
printf("The program was called wrong.\n");
}
return 0;
}
I tried to implement toward the idea of throwing in a pointer, just in
case a callee requires it. That's not what the subject of this thread
was, and I've marked it solved so that no one is obligated to hang
around to help the hapless OP. I won't pursue it here because my
asbestos suit is giving out.
I was hoping to see a response from C, but didn't quite get there. Perl
caller after sig.
--
fred
use warnings;
use strict;
use 5.010;
use WWW::Mechanize::GZip;
use HTML::TableExtract;
#use HTML::TableExtract qw(tree);
use open ':std', OUT => ':utf8';
use Prompt::Timeout;
use constant TIMEOUT => 3;
use constant MAXTRIES => 30;
## redesign for solar eclipse of aug 21, 2017
my $site = 'http://www.fourmilab.ch/yoursky/cities.html';
my $mech = 'WWW::Mechanize::GZip'->new;
$mech->get($site);
$mech->follow_link( text => 'Portland OR' );
my $before_bound = 2457987.04167; #before conjunction
my $after_bound = 2457988.0; #after conjunction
$mech->set_fields(qw'date 2');
my $moon_seconds = 5;
my $sun_seconds = 3;
my $upper = $after_bound;
my $lower = $before_bound;
my $equal;
my $equal_sec;
my $now_string = localtime;
my $filename = '3.neptune.txt';
open( my $jh, '>>', $filename ) or die "Could not open file '$filename' $!";
say $jh "Script executed at $now_string";
say $jh " attempt upper lower guess";
my $attempts = 1;
## hard code 2 objects to look at
my $e1 = 5; #moon
my $e2 = 2; #sun
## hard code column
my $column = 1; #right ascension
while ( ( $sun_seconds != $moon_seconds ) ) {
my $default = ( ( $attempts >= MAXTRIES ) ) ? 'N' : 'Y';
my $answer = prompt( "Make query number $attempts?", $default,
TIMEOUT );
exit if $answer =~ /^N/i;
my $guess = closetohalf( $upper, $lower );
say "to server is $attempts $upper $lower $guess ";
say $jh "to server is $attempts $upper $lower $guess ";
$mech->set_fields( jd => $guess );
$mech->click_button( value => "Update" );
my $te = 'HTML::TableExtract'->new;
$te->parse( $mech->content );
my $table = ( $te->tables )[3]; #ephemeris table
# looking to get the whole row
my $row_ref1 = $table->row($e1);
my @row1 = @$row_ref1;
say $jh "row1 is @row1";
my $row_ref2 = $table->row($e2);
my @row2 = @$row_ref2;
say $jh "row2 is @row2";
my $moon = $row1[$column];
say "right ascension of moon $moon";
say $jh "right ascension of moon $moon";
my $sun = $row2[$column];
say "right ascension of sun $sun";
say $jh "right ascension of sun $sun";
$moon_seconds = string_to_second($moon);
say "moon seconds is $moon_seconds";
say $jh "moon seconds is $moon_seconds";
$sun_seconds = string_to_second($sun);
say "sun seconds is $sun_seconds";
say $jh "sun seconds is $sun_seconds";
if ( $sun_seconds < $moon_seconds ) {
$upper = $guess;
}
elsif ( $moon_seconds < $sun_seconds ) {
$lower = $guess;
}
else {
$equal = $guess;
say "equal, while condition fails at julian day $equal";
say $jh "equal, while condition fails at julian day $equal";
$equal_sec = $moon_seconds;
}
$attempts++;
}
say $jh "equal seconds is $equal_sec";
my $return_pointer = pass_to_c( $equal );
say "return pointer pointed to $$return_pointer";
# re-design 4-29-17 for solar eclipse
sub string_to_second {
my $string = shift;
my $return = 9000;
if ( my $success = $string =~ /^(\d*)h\s+(\d*)m\s+(\d*)s$/ ) {
$return = 3600 * $1 + 60 * $2 + $3;
}
else {
say "string was misformed";
}
return $return;
}
sub pass_to_c {
my $string = shift;
my $ref_to_c;
my $ref_from_c;
system "./jup $string";
return $ref_from_c;
}
sub closetohalf {
my ( $up, $low ) = @_;
$low + ( $up - $low ) * ( 0.4 + rand 0.2 );
}