- now I would like to load this file so that its content is encapsulated either in a hash table or in an object. E.g. in file main.pl, I would like to use a function such as capsule that would create such object:

Smells strongly like an XY problem, but you can require your file. Then if its code is within an own package, the latter's stash will be filled with variables and functions. (If you want -and you have to- stay under strict, then use our for the declarations.) Then you can map (perhaps by means of an actual map) that stash into a generic hash. Very clumsy and no reason why one would want to do that, if you ask me, but certainly doable. If you want to have object access, instead, it's enough that you enclose again the code of your file in a package declaration and that obj is blessed into that package. Just very few lines of code to add. Of course this won't also make for automatic access to $obj->text out of a bare $text package variable, but if you really really want that you will need to implement you accessor creating logic mangling the symbol table: again, no really good reason for doing so that I can imagine of.


In reply to Re: Encapsulate function and variables by blazar
in thread Encapsulate function and variables by nictam

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.