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

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.