Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 contemplating the Monastery: (10)
As of 2014-07-22 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (109 votes), past polls