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.
It's important to pay attention about the declaration/definition order to avoid unexpected result.#!/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
How do I "use" it in the main scriptlet file?There are some ways:
Aliasing the symbol tables you're going to access so you don't have to fully qualify the symbol name. kyle has shown some examples.
package main; *set_scale = \&my_temp_convert::set_scale; set_scale(1);
sub new { bless {} } sub set_scale { my $self = shift; $curr_scale = shift; } # later..... package main; my $temp = my_temp_convert->new; $temp->set_scale(1);
package main; my_temp_convert->import; set_scale(1)
package main; no strict 'refs'; # this is necessary for my $sym (keys %my_temp_convert::) { *{"::$sym"} = *{"my_temp_convert::$sym"}; } set_scale(1);
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,
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?*main:: = *my_temp_convert::;
Don't focus on the triviality of my "temperature convert" packageWell, 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,
instead ofsub 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.sub set_scale{ curr_scale=shift} sub set_temp($){#convert to C if needed, and store in "curtmp"}
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |