I've been learning about the Perl API lately, and just playing around a bit. I haven't coded in C in many years, so a lot of it is pretty much guessing.

The below code works as is, and I understand what's happening (per perlapi and perlcall), but I have a few questions.

Before the loop over the file, I define a char of 256 bytes. I'm pretty sure if a line length is longer than that, it'd be a buffer overrun (but I'm not positive).

What I'd like to do, is instead of a char, I'd like to create an empty newSV in its place, but I don't understand the size_t size reference for STRLEN arg in the docs. When I do sizeof(size_t), I get 8 bytes, which is no where near long enough. Can someone please point out my err?

My next issue is that when I do define manually a newSVpvf() I can't figure out to put it onto the stack properly; the perl sub never sees it.

Could anyone in the know please help me understand how to create a new empty newSV() and use it in char* line's place, and beyond that, an example of what I'd need to change once this is done?

use warnings; use strict; use Inline 'C'; c_replace('a.txt'); sub modify_from_c { my $line = shift; $line =~ s/0/5/; return $line; } __END__ __C__ #include <stdio.h> int c_replace(char* fname){ FILE *fh; fh = fopen(fname, "r"); if (! fh){ printf("Couldn't open file %s for reading.\n", fname); return 0; } char* line[256]; while (fgets(line, sizeof(line), fh)){ dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(line, sizeof(line)))); PUTBACK; int return_count = call_pv("modify_from_c", G_SCALAR); if (return_count > 1){ croak("invalid return count from modify_from_c()\n"); } SPAGAIN; char* modified_line = POPp; printf("c orig: %sc new: %s", line, modified_line); PUTBACK; FREETMPS; LEAVE; } fclose(fh); }

a.txt simply contains:

10 20 30 40 50

In reply to Calling perl sub from a C loop using an SV as loop variable (as opposed to char) by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.