I really wouldn't recommend using packages this way. However, if you're intent on doing it this way, this seems to work:
use strict; use warnings; use Test::More 'no_plan'; package my_temp_convert; my $curr_scale=0; #centigrade, 1=faren my $cur_ctemp=0; sub set_scale{ $curr_scale=shift} sub set_temp{ $cur_ctemp =shift} sub c {return $cur_ctemp;} sub f {return 32+(9*$cur_ctemp)/5} package main; *set_scale = \&my_temp_convert::set_scale; *set_temp = \&my_temp_convert::set_temp; *c = \&my_temp_convert::c; *f = \&my_temp_convert::f; set_scale(1); set_temp(0); is( f(), 32, 'f() returns 32' ); is( c(), 0, 'c() returns 0' );
You might also want to look at Re^3: Beyond Inside-Out (details), which has some interesting examples.
Generally, I'd advise using a real object for this kind of job. Then you'd do:
my $temp = My::Temp::Thing->new(); $temp->set_c(0); is( $temp->get_f(), 32, '0 c is 32 f' );
In that case, you don't clutter your current name space with junk from another package. You can have multiple temperature objects instead of just One. Etc.
In reply to Re: multiple "sub" packages in 1 file: possible? how?
by kyle
in thread multiple "sub" packages in 1 file: possible? how?
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |