CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Q: function pass in struct pointer, come back with data filled

 



ningji
Novice

Jul 26, 2013, 8:31 PM

Post #1 of 3 (566 views)
Q: function pass in struct pointer, come back with data filled Can't Post

in c code i can do this
func(struct aaa *)
{
aaa->f1 = 1;
aaa->f2 = "abc";
}

how to do this in perl pls ?


Laurent_R
Veteran / Moderator

Jul 27, 2013, 5:07 AM

Post #2 of 3 (558 views)
Re: [ningji] Q: function pass in struct pointer, come back with data filled [In reply to] Can't Post

Hi, not sure I understand what you want, but it seems that you want to assign a structure within a function.

You could use a hash and pass a reference to the hash to your function. Something like this:


Code
my %aaa; 
fill_hash(\%aaa);
print $aaa{'f1'}; # prints 1
# ...

sub fill_hash {
my $aaa_hash_ref = shift;
$$aaa_hash_ref{'f1'} = 1; # if you prefer, it could also be: $aaa_hash_ref->{'f1'} = 1
$$aaa_hash_ref{'f2'} = 'abc';
}



BillKSmith
Veteran

Jul 27, 2013, 8:08 AM

Post #3 of 3 (551 views)
Re: [ningji] Q: function pass in struct pointer, come back with data filled [In reply to] Can't Post

It is not possible to do exactly what you are asking because perl's concept of type is much different from C's. Perl's hash is similar to C's struct, but not at all the same. Perl's hash keys are very similar to C's structure members. Perl's reference is similar to C's pointer, but again, not the same. Perl's function prototypes appear to be similar to C's, but they are not! (In fact, they are almost never needed or appropriate.)

Laurent's answer is probably the best perl solution to the same type of problem that your c function would be used for.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives