I'd like to highlight some things: Firstly, embedding packages in a script, or having multiple packages in the same file are not unusual. One can even use the technique of embedded-package in production for practical reason.
So is there anyway to define a separately named package and not put it in another file?
Yes, exactly as you give in your example. Just put the package PackageName; line to define a new package scope, or switch back to a previously defined package.
#!/usr/bin/perl use strict; use warnings; use CGI; # this is by default in package main scope package TempConverter; # define necessary stuff for TempConverter # and start its package scope # you can even do this, to.... package CGI; # ...extend the CGI package, but this is not # recommended package TempConverter; # back in TempConverter scope and add # something you need package main; # now in main scope, ready for action! # the safest place to do things
It's important to pay attention about the declaration/definition order to avoid unexpected result.
How do I "use" it in the main scriptlet file?
There are some ways:

Note: I remember I once used a single assignment to basically do the same with what for does above, but I can't find references about that in the official docs anymore. The assignment below,

*main:: = *my_temp_convert::;
gives me a "Modification of a read-only value" fatal exception. I'm not sure if that's the correct syntax I used or thing has changed. Any monk knows what happens?
Don't focus on the triviality of my "temperature convert" package
Well, at least use valid syntax even for example. Or, state that your example uses pseudo code. Please remember that we're using PerlMonks to help each other. Supplying some example code indicates that you put some efforts and help others to help you. Things in <code></code> tags are downloadable, and allow other people to try your code. So I would appreciate if you wrote,
sub set_scale{$curr_scale=shift} sub set_temp($){ #convert to C if needed, and store in "curtmp" }
instead of
sub set_scale{ curr_scale=shift} sub set_temp($){#convert to C if needed, and store in "curtmp"}
People will try to work based on your code you supply to closely match the context of your problem so it's prefered if your code is syntatically correct (unless the problem is with the syntax itself), so people don't have to modify a lot of things.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: multiple "sub" packages in 1 file: possible? how? by naikonta
in thread 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.