Re: AW: Accessing variables from inside a C-XS

[email protected] (Josef Moellers)
Newsgroups perl.xs
Message-ID <[email protected]>
Am 10.12.2010 schrub Bolhar-Nordenkampf Ferdinand:

> Hi,
> 
>>     SV*  perl_get_sv("package::varname", FALSE);
>>
>> This returns NULL if the variable does not exist.
>>
>> If you want to know if this variable (or any other SV) is actually
>> defined, you can call:
>>
>>     SvOK(SV*)
> 
> Note that this works for global (package) variables only.
> 
> Is there a similar way to access lexical variables?

I doubt it. I've had a look at
http://perl.plover.com/FAQs/Namespaces.html
and it says that there's no way to access a lexical variable that way.

However, having read that and since I'd like to use this to configure a
timeout value for a SCSI package, I think the best, and cleanest,
approach is to have a package variable and maybe even make that private
to the package and allow access only via an oo-style sub:

sub timeout {
    my ($new_timeout) = @_;

    my $old_timeout = $timeout;
    if (defined $new_timeout) {	# Maybe also check that there's only one
parameter
	if ($new_timeout =~ /^\d+$/) {
	    $timeout = $new_timeout;
	}
    }
    return $old_timeout;
}

-- 
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.