in reply to Re^2: multiple "sub" packages in 1 file: possible? how?
in thread multiple "sub" packages in 1 file: possible? how?

Well, this works too, if you really want to go through Exporter. I don't think you're going to be able to use as you would if the package were in a separate file since use really implies other fileness.

use strict; use warnings; use Test::More 'no_plan'; package my_temp_convert; require Exporter; our @ISA = ('Exporter'); our @EXPORT = qw(set_temp set_scale c f); 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; Exporter::import( 'my_temp_convert' ); set_scale(1); set_temp(0); is( f(), 32, 'f() returns 32' ); is( c(), 0, 'c() returns 0' );

Replies are listed 'Best First'.
Re^4: multiple "sub" packages in 1 file: possible? how?
by chromatic (Archbishop) on Aug 05, 2007 at 06:45 UTC
    Exporter::import( 'my_temp_convert' );

    Careful! import() is a method, so that line tries to import code from a package named my_temp_convert.

      That's the intention, importing symbols from my_temp_convert into main, and I believe that kyle is aware of it. It's the same with my_temp_convert->import call. Both calls are using my_temp_convert as the first argument to the subroutine/method.

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

        Calling methods as functions has the nasty tendency to break your code (and especially my code). In this case when the correct code is both shorter and clearer!