Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3362748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:18:43+00:00 2026-05-18T03:18:43+00:00

This problem is so simple I can feel the RTFM’s coming. However, I’ve been

  • 0

This problem is so simple I can feel the RTFM’s coming. However, I’ve been looking at the docs (Inline, Inline-C, Inline-C-Cookbook ) all morning and I can’t figure out how to solve this problem.

I want to use inline C, but I don’t want to have C code in the same file as my perl code.

(Emacs doesn’t like having two languages in one file. In principle this is a matter of convenience, but in practice I’m having to edit my C in one file then copy-paste it into my perl script.)

Here is working perl:

#!/usr/bin/perl

use Inline C => DATA;
use strict;
use warnings;
use List::Util qw(sum);
use feature qw(say);

my @array = (1..10);
say "native perl: ", sum(@array), ", Inline C: ", sum1(\@array);

__END__
__C__

double sum1(AV* array) {
  int i;
  double sum = 0.0;
  for (i=0; i<=av_len(array); i++) {
    SV** elem = av_fetch(array, i, 0);
    if (elem != NULL)
      sum += SvNV(*elem);
  }
  return sum;
}

(thanks to mobrule for getting me this far.)

I want to move all of the C code (or as much as possible) into a separate header file.

What I can do is put sum1 into a header, and do this:

# same perl as above except now say sum2 instead of sum1
__END__
__C__
#include "sum.h"

double sum2(AV* array) {
    sum1(array);
}

This is good enough as I no longer have to edit the C in perl-mode, but I wonder if there isn’t a more elegant solution to this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:18:44+00:00Added an answer on May 18, 2026 at 3:18 am

    You can put your C code in a separate file and use Inline::bind to load it at runtime

    use Inline;
    use File::Slurp;
    
    my $data = read_file('source.c');
    Inline->bind(C => $data);
    

    or loading the source code in a BEGIN {} block to bind it at compile time

    my $data;
    use File::Slurp;
    BEGIN {
        $data = read_file('source.c');
    }
    use Inline C => $data;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this has got to be a super simple problem. I just can't seem
This may be a very simple problem, but I couldn't find an answer googleing
I am sure this is a very simple problem, but I am new to
I've never learnt JavaScript, but I imagine this is quite a simple problem. Just
This seems like a very simple and a very common problem. The simplest example
This problem crops up every now and then at work. Our build machine can
This problem has been solved thanks to your suggestions. See the bottom for details.
This problem has been kicking my butt for a few days now. I have
Alright, this one (3a; sample problem with provided answer) has got me scratching my
This problem occurred during daylight saving time change. After the change occurred, we've noticed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.