dissident has asked for the wisdom of the Perl Monks concerning the following question:
I am a novice and try to use structs. However, I seem unable to find out how to allocate one. Having spent several hours reading the perl documentation, the Class::Struct documentation and searching the webs, I fearfully decided to approach the sacred halls of Perl Monastery.
Could a fellow monk please help a novice find out how what is wrong with the following script?
use strict; use warnings; use Class::Struct; # include <structs> struct( document => { fileID => '$', # $ = scalar = int filename => '@', # @ = array (of char) = string tags => '@' # tags - array of strings }); # all of these three attempts to allocate memory fail: #my $doc = document->new( ); # produces: Can't locate object metho +d "fileID" via package "doc" (perhaps you forgot to load "doc"?) at t +estsnippet01.pl line 18. #my $doc = new( 'document'); # produces: Undefined subroutine &mai +n::new called at testsnippet01.pl line 12. #my $doc = new( document); # produces: Bareword "document" not a +llowed while "strict subs" in use at testsnippet01.pl line 13. my $doc = <WhatGoesHere?> # Hopefully a Perl saint knows what g +oes here... doc->fileID( 123 ); doc->filename( 'SampleFileName' ); doc->tags( 'tag1', 'tag2', 'tag3' ); print "File ID: ", doc->fileID, "\n"; print "Filename: ", doc->filename, "\n"; print "Tags: ", doc->tags, "\n";
I already looked into the alternative using associative arrays, but it would be much cleaner and effective to use structs like in C for my purposes...
So, any idea what the correct spell to allocate a struct could be?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to allocate a struct?
by Athanasius (Archbishop) on Oct 28, 2014 at 12:23 UTC | |
by dissident (Beadle) on Oct 28, 2014 at 13:28 UTC | |
by perlron (Pilgrim) on Oct 28, 2014 at 14:36 UTC | |
by dissident (Beadle) on Oct 28, 2014 at 21:10 UTC | |
by BrowserUk (Patriarch) on Oct 28, 2014 at 21:20 UTC | |
| |
Re: How to allocate a struct?
by Loops (Curate) on Oct 28, 2014 at 12:02 UTC | |
Re: How to allocate a struct?
by perlron (Pilgrim) on Oct 28, 2014 at 12:11 UTC |