XS::Framework::Manual::SVAPI::Scalar - XS::Framework Scalar C++ class reference
The Scalar class is the wrapper around Perls SV* object, which helds either primitive value like number or string, or reference to object, array or hash or Glob. So, is base class for Simple, Ref and Glob.
Scalar
SV*
Glob
Simple
Ref
As with Sv, it might hold an underlying Perl SV* or might not.
Sv
The notable difference from all other classes is that Scalar might held undef and it is not considered empty.
undef
static const Scalar undef; static const Scalar yes; static const Scalar no;
Out of the box Scalar offers a few constants for Perl's undef and true and false values.
true
false
static Scalar create()
The create method return new non-empty Scalar, which holds zero-length string. It is opposite to the constructor with nullptr below, which creates empty scalar:
create
nullptr
Scalar (std::nullptr_t = nullptr) {}
The other constructors and helper methods just wrap existing Perl scalar SV* or GV* into Scalar object:
GV*
static Scalar noinc (SV* val) static Scalar noinc (GV* val) Scalar (SV* sv, bool policy = INCREMENT) Scalar (GV* sv, bool policy = INCREMENT)
If SV* pointing to undef is porovided, it will be held and Scalar object will not be considered empty.
Copy and move-constructore are also available:
Scalar (const Scalar& oth) Scalar (Scalar&& oth) Scalar (const Sv& oth) Scalar (Sv&& oth)
It it possible to create Scalar object from CallProxy invocation:
CallProxy
Scalar (const CallProxy& p)
however, please note, that the CallProxy will be called in scalar context.
Scalar& operator= (SV* val) Scalar& operator= (GV* val) Scalar& operator= (const Scalar& oth) Scalar& operator= (Scalar&& oth) Scalar& operator= (const Sv& oth) Scalar& operator= (Sv&& oth) Scalar& operator= (const CallProxy& p)
The assignment operators are complementaty to the constructors above. They inherit behaviour from Sv, including NULL-safety. The previously held SV* will be dec-remented.
dec
void set (SV* val) void set (GV* val)
The set method directly assigns the value to the underlying SV*, bypassing all checks. Use the method with caution.
set
Theere are zero-cost NULL-safe getters:
template <class T = SV> one_of_t<T,SV,GV>* get () const
This are NULL-safe methods.
void upgrade (svtype type)
Tries to upgrade SV* into the specified type. Exception is thrown if the variable is already marked as readonly or upon attempt to upgrade defined scalar (non-undef) into more than SVt_PVMG.
SVt_PVMG
This is NULL-unsafe method.
template <class T = panda::string> T as_string () const
Stringizes the current Scalar object. Valid if the held Scalar can be constructed as Simple object, e.g. as number or string. Otherwise exception is thrown. The <T> parameter can be std::string, std::string_view or panda::string.
std::string
std::string_view
panda::string
template <class T = int> T as_number () const
Numberifies the current Scalar object. Valid if the held Scalar can be constructed as Simple object, e.g. as number or string. Otherwise exception is thrown. The <T> parameter can be any arithmetic type, conforming
std::is_arithmetic<T>
concept from C++ library.
XS::Framework
XS::Framework::Manual::SVAPI
XS::Framework::Manual::SVAPI::Sv
XS::Framework::Manual::SVAPI::Simple
XS::Framework::Manual::SVAPI::Ref
XS::Framework::Manual::SVAPI::Glob
To install XS::Framework, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XS::Framework
CPAN shell
perl -MCPAN -e shell install XS::Framework
For more information on module installation, please visit the detailed CPAN module installation guide.