develooper Front page | perl.perl5.porters | Postings from January 2012

XS help

Thread Next
From:
H.Merijn Brand
Date:
January 22, 2012 05:41
Subject:
XS help
Message ID:
20120122144059.7d661438@pc09.procura.nl
What is the XS equivalent (modern perl only is OK) of


{				# New scope
    local $/ = $some_eol;	# localize $/
    do_something ();		# do something with localized eol
    }				# leave scope

I currently have this

    {	STRLEN		result, rslen;
	const char	*rs = NULL;

	dSP;

	require_IO_Handle;

	PUSHMARK (sp);
	EXTEND (sp, 1);
	PUSHs (src);
	PUTBACK;

	csv->eol_pos = -1;
	if (csv->eolx || csv->eol_is_cr) {
	    rs = SvPOK (PL_rs) || SvPOKp (PL_rs) ? SvPV_const (PL_rs, rslen) : NULL;
	    sv_setpvn (PL_rs, (char *)csv->eol, csv->eol_len);
	    }

	result = call_sv (m_getline, G_SCALAR | G_METHOD);

	csv->tmp = result ? POPs : NULL;
	if (csv->eolx || csv->eol_is_cr) {
	    if (rs)
		sv_setpvn (PL_rs, rs, rslen);
	    else
		SvPOK_off (PL_rs);
	    }

	SPAGAIN;
	PUTBACK;
        }

But I get this fail (from RT):
https://rt.cpan.org/Ticket/Display.html?id=74216

$ cat rt74216.pl
use strict;
use warnings;
use Test::More;
use Text::CSV_XS;

slurp_check ();

my $crlf = "\015\012";
my $csv_data = "a,b,c" . $crlf . "1,2,3" . $crlf;
open my $csv_fh, "<", \$csv_data or die $!;
my $csv = Text::CSV_XS->new ({ eol => $crlf });
my $csv_parse = $csv->getline ($csv_fh);

## now prints 1
slurp_check ();

## restore $/ to get 100 again
{ local $/ = "\n"; slurp_check () }

done_testing ();

sub slurp_check
{
    my $data = join "\n", 1 .. 100;
    open my $fh, "<", \$data or die $!;
    is (scalar @{[<$fh>]}, 100);
    close $fh;
    }
$ prove -vwb sandbox/rt74216.pl
sandbox/rt74216.pl ..
ok 1
not ok 2

#   Failed test at sandbox/rt74216.pl line 28.
#          got: '1'
#     expected: '100'
ok 3
1..3
# Looks like you failed 1 test of 3.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests

Test Summary Report
-------------------
sandbox/rt74216.pl (Wstat: 256 Tests: 3 Failed: 1)
  Failed test:  2
  Non-zero exit status: 1
Files=1, Tests=3,  0 wallclock secs ( 0.01 usr  0.00 sys +  0.01 cusr  0.00 csys =  0.02 CPU)
Result: FAIL

-- 
H.Merijn Brand  http://tux.nl   Perl Monger  http://amsterdam.pm.org/
using perl5.00307 .. 5.14   porting perl5 on HP-UX, AIX, and openSUSE
http://mirrors.develooper.com/hpux/        http://www.test-smoke.org/
http://qa.perl.org   http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About