The below code works as is

Not quite.

char* line[256]; // An array of 256 pointers, sizeof(line) == 2048
should be
char line[256]; // An array of 256 chars, sizeof(line) == 256
and
newSVpv(line, sizeof(line)) // length($line) == 2048 (or 256)
should be
newSVpv(line, strlen(line)) // length($line) == 3 -or- newSVpv(line, 0) // length($line) == 3

I'm pretty sure if a line length is longer than that, it'd be a buffer overrun (but I'm not positive).

No overrun. You'll just get part of the line in one loop, and the rest in subsequent loop passes.

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

If you want to avoid truncating lines, you can use getline instead of fgets.

char* line = NULL; ssize_t line_size; while (1) { SV* line_sv; ssize_t len = getline(&line, &line_size, fh); if (len < 0) last; line_sv = newSVpvn(line, len); ... } if (line) free(line);

Could anyone in the know please help me understand how to create a new empty newSV() and use it in char* line's place

Why???

while (1) { // Actually allocates at least 256. SV* line_sv = newSV(255); // We know line_sv has a string buffer, so we can use SvPVX. char* line = SvPVX(line_sv); if (!fgets(line, 256, fh)) { break; } SvCUR_set(line_sv, strlen(line)); SvPOK_on(line_sv); ... // The callback may have changed line_sv, // so line might no longer be valid. line = SvPVbyte_nolen(line_sv); printf("c orig: %sc new: %s", line, modified_line); ... }

Maybe you want wanted to reuse line_sv instead of creating a new one for each loop pass, but that can be tricky because the callback could have changed it.


In reply to Re: Calling perl sub from a C loop using an SV as loop variable (as opposed to char) by ikegami
in thread 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.