Last active
December 25, 2015 19:08
-
-
Save run4flat/7025130 to your computer and use it in GitHub Desktop.
Basic use of C::Blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
package C::Blocks; | |
use Devel::Declare; | |
sub import { | |
my $class = shift; | |
my $caller = caller; | |
Devel::Declare->setup_for( | |
$caller, | |
{ method => { const => \&parser } } | |
); | |
no strict 'refs'; | |
*{$caller.'::C'} = sub (&) {}; | |
} | |
sub parser { | |
# Pull all of the text into the buffer until we find the matching right | |
# curly brace. This assumes that nothing of any interest follows the closing | |
# curly brace. | |
my $C_code = ''; | |
my $brace_count = 0; | |
do { | |
my $line = Devel::Declare::get_linestr(); | |
print "line was [$line]\n"; | |
$brace_count += $line =~ tr/{//; | |
$brace_count -= $line =~ tr/}//; | |
$C_code .= $line; | |
# Inject whitespace for the moment | |
Devel::Declare::set_linestr("\n"); | |
} while ($brace_count > 0); | |
# For testing | |
$C_code =~ s/\n/ NEWLINE /g; | |
Devel::Declare::set_linestr("; print 'C code was $C_code';\n"); | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use C::Blocks | |
my $name = "David"; | |
# This will eventually create an op that gets executed inline | |
C { | |
printf("This is C code. My name is %s\n", SvPV_nolen($name)); | |
} | |
# C blocks that are not immediately followed by french braces will | |
# eventually declare a C function that is available to other C blocks | |
# in the same lexical scope. I'm not sure if this will be able to cleanly | |
# handle lexically scoped variables in the way that inline code does. | |
C void say_hello (char * name) { | |
printf("Hello, %s", name); | |
} | |
print "All done!\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment