Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: C-Style Struct?

by xiaoyafeng (Chaplain)
on Aug 08, 2012 at 14:02 UTC ( #986286=note: print w/ replies, xml ) Need Help??


in reply to C-Style Struct?

In general, You don't need to create C-style struct in perl in favor of a hash. But There is indeed several ways to do this, below is one of those:

package C_struct; sub new{ my $self = shift; return bless {'counting_number' => shift, 'name' => shift, 'stuff_list' => shift }, $self; } package main; use Data::Dumper; my $cc = C_struct->new(123,'bb', [1,2,3]); my $dd = C_struct->new(234,'cc', [2,3,4]); print $cc->{name}; print $dd->{name}; print Dumper $dd->{stuff_list}; 1;
Always as a alternative, you can searcg modules on CPAN to reach what you want instead of doing it by hand.

UPDATE

modify code to be more intuitive.




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction


Comment on Re: C-Style Struct?
Download Code
Re^2: C-Style Struct?
by Arunbear (Parson) on Aug 08, 2012 at 17:24 UTC
    Your code example doesn't allow access by name, so what's the advantage of it?

      You could add accessors to the object. Like this:

      sub count { my $self = shift; return $self->[0] unless @_; $self->[0] = shift; } # ... $cc->count(42); print $cc->count;
      changed it, Thanks for your reminding.;)




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        But now I can see no advantage to using your example over just using a hash ;)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-07-07 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (85 votes), past polls