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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.