Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Perl Embedded: Setting %ENV, STDIN and Workingdirectory

by esskar (Deacon)
on Jul 31, 2005 at 23:28 UTC ( #479752=note: print w/replies, xml ) Need Help??

in reply to Perl Embedded: Setting %ENV, STDIN and Workingdirectory

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? :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479752]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2023-02-02 14:27 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (19 votes). Check out past polls.