Last Updated: July 15, 2016
·
1.92K
· jmcveigh

Class::Struct in Perl

Class::Struct grants access to one subroutine titled struct.

Using this subroutine is a handy way to get a blessed data structure in Perl.

The idea is that the data structure is given a class name as well as a list of hash keys and respective sigils.

I find this subroutine to be smooth as silk. This is a useful addition to the Modern Perl programmer's utility belt.

use Class::Struct;

struct Person {
    name => '$',
    age => '$',
    peers => '@',
};

my $p = Person->new();
$p->name("Jason Smythe");
$p->age(13);
$p->peers( ["Wilbur", "Ralph", "Fred"] ) ;

# or this way
@{$p->peers} = ("Wilbur", "Ralph", "Fred");

printf "At age %d, %s's first friend is %s.\n", $p->age, $p->name, $p->peers(0);

2 Responses
Add your response

I made this!

over 1 year ago ·

I ♥ what I do and I hope you do too!

over 1 year ago ·