value_slots#
Which value representations the runtime currently considers valid for the scalar $ref points at.
Synopsis#
use Peta::XS ':facts';
my @slots = value_slots(\42); # ('int')
my @slots = value_slots(\3.14); # ('num')
my @slots = value_slots(\'hi'); # ('str')
my @slots = value_slots(\\1); # ('ref')
my @slots = value_slots(\undef); # ()
What you get back#
In list context, a subset of qw(int num str ref) - the representations the runtime currently considers valid for this value. In scalar context, how many of them there are. A scalar can hold several at once: after a numeric string is used in numeric context it carries both its string and its numeric form, and a Scalar::Util::dualvar carries int and str by construction.
Contract#
This is an observation of optimization state, not of meaning: the same program may legitimately report different slot sets across runtimes and versions. Branch on it for diagnostics and teaching, never for program semantics.
Examples#
## watch a scalar accumulate representations
my $port = '8080';
say "@{[ value_slots(\$port) ]}"; # str
my $next = $port + 1; # numeric use caches the number
say "@{[ value_slots(\$port) ]}"; # int str
## dualvars hold two truths at once ($! is the classic case)
use Scalar::Util qw(dualvar);
my $dv = dualvar(5, 'five');
say "@{[ value_slots(\$dv) ]}"; # int str
## a classroom one-liner: show why "10" == 10 costs a conversion
## only the first time