Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Inline::C : passing parameters to functions, modifying by reference

by Anonymous Monk
on Jul 23, 2021 at 06:08 UTC ( #11135333=note: print w/replies, xml ) Need Help??


in reply to Inline::C : passing parameters to functions, modifying by reference

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re: Inline::C : passing parameters to functions, modifying by reference

Replies are listed 'Best First'.
Re^2: Inline::C : passing parameters to functions, modifying by reference
by bliako (Monsignor) on Jul 23, 2021 at 08:36 UTC

    Anonmonk, here is how but I can't see how this is related to Inline::C.

    Case2 (pass a scalar - not a ref) is obviously failing. But I don't think that should apply to Inline::C because AFAICT a scalar, like a scalarref and any ref, is passed as an SV * which I can affect its PV field within func() and insert an arrayref in there. A good warmup though thanks.

    use strict; use warnings; use Test::More; use Data::Dumper; my $correct_result = make_a_rowsxcols_array(5,3); print Dumper($correct_result); ### $#out = -1; # clears an array; # Case1: pass it an arrayref my $T = 'Case1'; my @out = (1,2,3); is( case1(\@out), 0, "$T: returned success."); is_deeply(\@out, $correct_result, "$T: correct result."); # Case2: pass it a scalar $T = 'Case2'; my $x = 123; is( case2($x), 0, "$T: returned success."); is_deeply($x, $correct_result, "$T: correct result."); # Case3: pass it a scalarref $T = 'Case3'; my $y = 123; is( case3(\$y), 0, "$T: returned success."); is_deeply($y, $correct_result, "$T: correct result."); done_testing(); sub case1 { # pass an arrayref, clear it and make it 5rowsx3cols my $out = $_[0]; return 1 unless ref($out) eq 'ARRAY'; $#{ $out } = -1; push @$out, @{ make_a_rowsxcols_array(5,3) }; return 0; } sub case2 { # pass a simple scalar, make it an arrayref my $out = $_[0]; return 1 unless ref($out) eq ''; $out = make_a_rowsxcols_array(5,3); return 0; } sub case3 { # pass a scalarref, fill it with an arrayref to 5rowsx3cols my $out = $_[0]; return 1 unless ref($out) eq 'SCALAR'; undef $$out; $$out = make_a_rowsxcols_array(5,3); return 0; } sub make_a_rowsxcols_array { [ map { [ (42)x$_[1] ] } 1..$_[0] ] }

    bw, bliako

Re^2: Inline::C : passing parameters to functions, modifying by reference
by eyepopslikeamosquito (Bishop) on Jul 24, 2021 at 03:24 UTC
Re^2: Inline::C : passing parameters to functions, modifying by reference
by LanX (Sage) on Jul 23, 2021 at 07:53 UTC
    And more importantly, how would you explain it in pure Vietnamese?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-10-21 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (83 votes). Check out past polls.

    Notices?