in reply to Re^7: XS/Inline::C concat *any* two SVs. (!all)
in thread XS/Inline::C concat *any* two SVs.

sv_catsv handles "get magic", so overloaded strings shouldn't be a problem. It doesn't handle "set magic", though, so if the first scalar is tied and it does something funny with STORE -- like store an uppercased version of the input -- that won't happen.

#!/usr/bin/perl package TiedScalar; use strict; use warnings; use base qw( Tie::Scalar ); sub TIESCALAR { my ( $classname, $initial_value ) = @_; return bless \$initial_value, __PACKAGE__; } sub FETCH { return ${ +shift } } sub STORE { my ($self, $value) = @_; $$self = uc($value); } package main; use strict; use warnings; use Inline C => << '__C__', NAME => 'test', CLEAN_AFTER_BUILD => 0; #include <stdio.h> SV* test( SV *a, SV *b ) { if( SvREADONLY( a ) ) a = newSVpv( SvPVX( a ), 0 ); else SvREFCNT_inc( a ); if( !SvOK( a ) ) sv_setpvn( a, "", 0 ); sv_catsv( a, b ); return a; } __C__ my ( $q, $p ); tie $q, 'TiedScalar', "bill"; tie $p, 'TiedScalar', "fred"; print test( $q, $p ) . "\n"; $q .= $p; print "$q\n";

As for the exotic values via SvPVX, I can't think of an example that would cause any more problems than say, packed ints.

As for nulls in read-only scalars, I would think sv_catsv would handle that case, no?

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Replies are listed 'Best First'.
Re^9: XS/Inline::C concat *any* two SVs. (!all)
by tye (Sage) on May 31, 2006 at 14:59 UTC
    As for nulls in read-only scalars, I would think sv_catsv would handle that case, no?

    This doesn't

    a = newSVpv( SvPVX( a ), 0 );

    It is another place that doesn't handle magic either.

    I don't know what you mean by "any more problems than say, packed ints". Does this mean you've thought of a case where SvPVX() doesn't return a pointer to the string value of a scalar? Surely there are some. Isn't that problem enough?

    - tye