Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: C-Style Struct?

by xiaoyafeng (Deacon)
on Aug 08, 2012 at 14:02 UTC ( [id://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

Replies are listed 'Best First'.
Re^2: C-Style Struct?
by Arunbear (Prior) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://986286]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-19 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found