Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Stream Class for Calling Perl from C++


January 2002/A Stream Class for Calling Perl from C++/Listing 2

Listing 2: PerlStream.cc

bool PerlValue::isInt() const // line 1
{ return SvIOK(value); }

int PerlValue::Int() const // line 4
{
  if (SvIOK(value) || SvNOK(value) || SvPOK(value)) {
    return SvIV(value);
  } else if (exception_flags & PerlStream::eBadCast) {
    string errstr = string("Casting from a Perl ") + typeStr() +
        " to an int";
    throw PerlBadCast(errstr);
  }
  return 0;         // Not a valid value, so make it 0
}
// ... Lots of other PerlValue functions

PerlStreamBuf::PerlStreamBuf(unsigned int flags)
  : streambuf(), mContext(eScalar), mExceptionFlags(flags) {}

PerlStreamBuf::~PerlStreamBuf()
{ freePerlValues(); }

int PerlStreamBuf::overflow(int c) // line 23
{ mBuffer += static_cast<char>(c); return c; }

streamsize PerlStreamBuf::xsputn(const char* s, streamsize n) // line 26
{ mBuffer.append(s, n); return n; }

bool PerlStreamBuf::execute() // line 29
{
  string err;
  dSP;
  I32 ax;
  
  freePerlValues(); // Free values from the previous eval
  if (mBuffer.length() == 0) return true;

  ENTER;
  SAVETMPS;
  PUSHMARK(sp);

  // Create the string we want to send to Perl, and send it
  sv_setsv(ERRSV, &PL_sv_undef);
  PUTBACK;
  SV *code = sv_2mortal(newSVpv(mBuffer.data(),mBuffer.size()));
  I32 nstackvals = perl_eval_sv(code, mContext|G_KEEPERR);

  // Now get the response
  SPAGAIN;
  if (SvTRUE(ERRSV)) {
    err = "Eval failed: " + mBuffer + ": " + SvPV(ERRSV, PL_na);
    for (I32 i = 0; i < nstackvals; ++i)
      POPs;
  } else {
    // Get the return values from the Perl stack.
    mRtnVals.resize(nstackvals);
    for (I32 i = nstackvals-1; i >= 0; --i) {
      SV* val = POPs;
      SvREFCNT_inc(val);
      mRtnVals[i] = val;
    }
  }
  PUTBACK;
  FREETMPS;
  LEAVE;

  // Initialize things for the next go around
  mBuffer.erase();
  mContext = eScalar;
  if (!errstr.empty()) {
    if (mExceptionFlags & PerlStream::eEvalFail)
      throw PerlEvalError(err);
    return false;
  } else {
      return true;
  }
}

void PerlStreamBuf::freePerlValues()
{
  vector<SV*>::iterator p;
  for (p = mRtnVals.begin(); p != mRtnVals.end(); ++p)
    if (*p) { SvREFCNT_dec(*p); *p = 0; }
  mRtnVals.clear();
}
— End of Listing —

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.