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?
In reply to How to allocate a struct? by dissident
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |