Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

C-Style Struct?

by Mr. Newb (Acolyte)
on Aug 08, 2012 at 13:40 UTC ( #986272=perlquestion: print w/ replies, xml ) Need Help??
Mr. Newb has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem that, in my mind, instantly calls for the use of a C-sytle struct. I have an array of some items, a name for that array, and a counting variable. I know that there are quite a few ways to overcome this without struct, such as an array of references to these items, or an array with the first two elements as the scalars I need, but these methods seem a bit contrived and awkward. Is there a way to do something like this:
struct thing { my $counting_variable; my $name; my @list_of_stuff; };
-Bewildered c++ user

Comment on C-Style Struct?
Select or Download Code
Replies are listed 'Best First'.
Re: C-Style Struct?
by BrowserUk (Pope) on Aug 08, 2012 at 13:48 UTC

    How about:

    my %thing = ( name => 'thing', count => 0, list => [] ); ++$thing{ count }; print $thing{ name }; print $thing{ list }[ 123 ];

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: C-Style Struct?
by Anonymous Monk on Aug 08, 2012 at 13:47 UTC
Re: C-Style Struct?
by xiaoyafeng (Chaplain) on Aug 08, 2012 at 14:02 UTC
    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.


    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

      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

Re: C-Style Struct?
by influx (Beadle) on Aug 08, 2012 at 18:51 UTC

    I'm not really familiar with C. But does this work like a struct?

    sub struct { my ($name, %args) = @_; localscope: { # we want to mangle the symbol table # so set 'no strict refs' no strict 'refs'; foreach my $a (keys %args) { *{"${name}::${a}"} = sub { my ($class, $val) = @_; # val means it needs updating if ($val) { nowarning: { no warnings 'redefine'; *{"${name}::${a}"} = sub { $val; }; } # end nowarning return $val; } return $args{$a} }; } } # end localscope } # Create a new struct struct 'Player' => ( count_var => 0, name => 'Zork', list_of_stuff => ['a', 'b'] ); print Player->name; # you can update a value in this "struct", like so Player->name('Zippy');

      Thinking about it, Class::Struct as mentioned in the first reply is probably a nicer option. Seems to do the same thing, but better. And it looks like it's included in perl5.16 - nice!

Re: C-Style Struct?
by Mr. Newb (Acolyte) on Aug 08, 2012 at 13:57 UTC
    To BrowserUK, I ran your code, the output is just 'thing'. I would expect it to print 123. What does that line ( print $thing{ list }[123]) do?
        My self has few answers, that's why I come here. How would I iterate through the array $thing{list}? ie, what would I write for a foreach loop?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://986272]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2015-11-29 22:37 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (754 votes), past polls