Arlekin has asked for the wisdom of the Perl Monks concerning the following question:

Good day. I have some trouble during writing the program. I got script that 100% throw exception if i run it from perl (perl test.pl). Script body see below:
sub register_vm { my ($config) = @_; my $connect_params = VMware::VmPerl::ConnectParams::new(); # Establish a persistent connection with server my $server = VMware::VmPerl::Server::new(); if (!$server->connect($connect_params)) { my ($error_number, $error_string) = $server>get_last_error(); die "Could not connect to server: Error $error_number: $error_stri +ng\n"; } if (!$server->register_vm($config)) { my ($error_number, $error_string) = $server>get_last_error(); die "Could not register of VM $config: Error $error_number: $error_ +string\n"; } undef $server; }

Then i wrote function in order to use that script from C++.
bool PerlWrapper::PerlRegisterVm(char* config, char* error) { bool bRet = true; dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(config, 0))); PUTBACK; perl_call_pv("register_vm", G_DISCARD|G_EVAL); SPAGAIN; if(SvTRUE(ERRSV)) { strcpy(error, SvPV(ERRSV, PL_na)); bRet = false; } PUTBACK; FREETMPS; LEAVE; return bRet; }

The block of exception handling (SvTRUE) never take control. Why? Thanks for any suggestions.

Replies are listed 'Best First'.
Re: Perlembed: problem with exception
by shmem (Chancellor) on Oct 25, 2006 at 13:23 UTC
    Not that I really knew what's happening (too little info?), but I suggest checking whether the function calls in your C++ code snippet actually succeed.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Perlembed: problem with exception
by syphilis (Archbishop) on Oct 26, 2006 at 02:05 UTC
    strcpy(error, SvPV(ERRSV, PL_na));

    Looks like you're trying to copy into the 'error' variable what is already in $@. You want to use sv_setpv instead of strcpy - see 'perldoc perlclib'.

    Here's a simplified Inline C example that works fine for me.
    use warnings; use Inline C => Config => BUILD_NOISY => 1; sub register_vm { die "register_vm() has died"; } use Inline C => <<'EOC'; SV * foo(char * config) { int bRet = 1; STRLEN n_a; dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(config, 0))); PUTBACK; perl_call_pv("register_vm", G_DISCARD|G_EVAL); SPAGAIN; if(SvTRUE(ERRSV)) { printf ("From C: %s\n", SvPV(ERRSV, n_a)); bRet = 0; } PUTBACK; FREETMPS; LEAVE; return newSViv(bRet); } EOC $ret = foo('arg'); print "Perl's \$\@ contains: $@\n\$ret: $ret\n";
    It outputs:
    From C: register_vm() has died at try.pl line 5. Perl's $@ contains: register_vm() has died at try.pl line 5. $ret: 0
    Not quite sure what your original aim was. Hopefully, with the help of 'perldoc perlapi' and the other docs I've already mentioned, you can work out how to modify that script to suit your needs. If not, post again.

    Cheers,
    Rob