in reply to multiple "sub" packages in 1 file: possible? how?

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!

Replies are listed 'Best First'.
Re^2: multiple "sub" packages in 1 file: possible? how?
by shmem (Chancellor) on Aug 05, 2007 at 08:59 UTC
    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?

    What happens is just what you noticed. The symbol table typeglob itself is read-only. Assigning the symbol table hash itself doesn't help either (because it clobbers the table):

    package foo; sub foo {print "bar"}; package main; %main:: = %foo::; print "$_ => $main::{$_}" for keys %main::; foo(); __END__ foo => *foo::foo Undefined subroutine &main::foo called at -e line 1.

    so the shortest way to do the assignment you want is using a "hash slice":

    @main::{ keys %foo:: } = values %foo::;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Hash slicing will surely do the job. But what I'm talking about is a single line normal assignment, like what I am sure I did in the past. Just like we do with individual symbols,
      *alias = *target; # so we have $alias = $target, @alias = @target # %alias = %target and so on
      but we can do it package-wise.
      Assigning the symbol table hash itself doesn't help either (because it clobbers the table)
      You are right, shmem. I really forget how I did what I'm trying to say, but I really did. So, until I find my old code, I will consider that it can't be done, and stick with the for loop or hash slicing as shmem shown for this particular task.

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

Re^2: multiple "sub" packages in 1 file: possible? how?
by perl-diddler (Chaplain) on Aug 05, 2007 at 10:25 UTC
    Will have to play with the various examples you mention.

    I didn't want to confuse the issue by posting code that might have other distractions in it that would detract from the concept I wished to understand. I wasn't so much interested in having a specific answer, relative to my current code, but wanted to elicit solutions to the concept that I could mull over.

    The current code is just a "mash" of trying some new stuff out...experimenting...GUI usage...etc. Sorry about the syntax errors in my example -- I wasn't intending it to be code -- just a descriptive example of what I was trying to do. FYIW, my script has no temperature functionality in it. It has to do with a simple script to display images via Tk, in a shuffled or random order where each image has a numeric rating that determines its chances of being selected -- while allowing me to change the ratings of one or more images, move backwards and forwards in the "random" sequence...etc. Just a toy, really, but a good experimentation ground for better coding & design...if you know what I mean... Linda

      One more way to do it:
      package my::foo; BEGIN { $INC{'my/foo.pm'} ||= 'dev/null'; } require exporter; # etc. package main; use my::foo;
      The addition to %INC tells use/require that the package is already in memory, so it won't try to load it again.

      Search, Ask, Know