I was playing around with the structure of my code in a perl script. I thought to myself, that I'd like to partition off some variables by putting a "package" around them. I.e. -- I'd like to do something like:
...prog....(main)....(statements in package main here).. use strict; use Readonly; ...then a package definition in middle of prog.... package my_temp_convert; our $VERSION = '.01'; require Exporter; our @ISA = ('Exporter'); our @EXPORT = qw(set_temp, set_scale, c, f); my curr_scale=0; #centigrade, 1=faren my cur_ctemp=0; sub set_scale{ curr_scale=shift} sub set_temp($){#convert to C if needed, and store in "curtmp"} sub c {return cur_temp;} sub f {return 32+(9*cur_temp)/5} #now back to main.... package main; # somehow "use the above, inline package, "temp_convert"; set_scale(1); set_temp(32); print "%d F is %d C\n", f(), c(); #prints "32 F is 0 C" #end prog
So is there anyway to define a separately named package and not put it in another file? How do I "use" it in the main scriptlet file? Don't focus on the triviality of my "temperature convert" package -- it's just to demonstrate what I'm talking about. In case anyone wonders why I would do such a thing 0 -- it's a program in "development" -- I might eventually break up separate packages into separate files if they are complex enough and "standalone", but for now, it's just a way of separating the "sub package" code from the main code. I may also want to use sub packages to create objects as I abstract the code more... Thanks for any insights....
Linda

In reply to multiple "sub" packages in 1 file: possible? how? by perl-diddler

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.