Okay; setting %ENV is rather simple.
To make it simple, i wrapped part of perls variable types and their functionality in a C++ Class.
template <class T>
class Var {
typedef Var<T> Base;
public:
bool ValidPtr() {
return m_var != NULL;
}
virtual Release() {
m_var = NULL;
}
void Attach(T *var) {
Release();
m_var = var;
}
T *Detach() {
T *retval = m_var;
m_var = NULL;
return retval;
}
virtual void Clear() = 0;
virtual void Undef() = 0;
operator T *() {
return m_var;
}
operator T *() const {
return m_var;
}
operator const T *() {
return m_var;
}
operator const T *() const {
return m_var;
}
protected:
Var(T *var) : m_var(var) {
}
protected:
T* m_var;
};
class Scalar : public Var<SV> {
public:
Scalar();
Scalar(IV iv);
Scalar(UV uv);
Scalar(double dv);
Scalar(const char *str, STRLEN len);
Scalar(const std::string & str);
Scalar(SV *sv);
Scalar(const Scalar & scalar);
~Scalar();
public:
void Clear();
void Undef();
};
class Hash : public Var<HV> {
public:
Hash();
~Hash();
public:
void Clear();
void Undef();
public:
Scalar Store(const std::string & key, const Scalar & scalar);
};
void Hash::Store(const std::string & key, const Scalar & scalar) {
if(ValidPtr()) {
SV **result = hv_store(
*this,
key.c_str(),
key.length(),
scalar,
0
);
}
}
then i wrote a function as part of my interpreter class that returns a hash object
Hash Interpreter::GetHash(const char *name, bool create /*=false*/) {
Hash hash;
hash.Attach(get_hv(name, create));
return hash;
}
And now i'm able to do the follwing fun stuff:
Perl::Hash perlenv = m_perl->GetHash("ENV");
if(perlenv.ValidPtr()) {
perlenv.Store("foo", Perl::Scalar("bar"));
perlenv.Store("bar", Perl::Scalar("foo"));
}
Fun, isn't it? :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.