Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

packages / modules in the same file

by jptxs (Curate)
on Nov 13, 2001 at 19:30 UTC ( #125061=perlquestion: print w/replies, xml ) Need Help??
jptxs has asked for the wisdom of the Perl Monks concerning the following question:

NOTE: before telling me how terrible and wrong what I'm trying to do here is, know that I am aware of this and you can see NOTE2 at the bottom of this post for the reasons I'm doing it this way. I know DBI. I know this is bad form. There are reasons...


Is it possible to use packages/modules all in one file? something like this:

#!/usr/bin/perl -w use strict; package try; use Exporter; use vars qw(@ISA @EXPORT_OK); @ISA = qw(Exporter); @EXPORT_OK = qw($sql); my $sql = 'select name from v$database;'; 1; system("echo \'$try::sql\' | sqlplus -s system/manager");
As you've guessed, that doesn't work. I've tried it without the Exporter stuff, after the call, using the OO syntax, putting in a use try; and some other dumber things. Am I barking up the wrong tree? Do you ABSOLUTELY have to have modules in different files?

We speak the way we breathe. --Fugazi

NOTE2: the reason for this is I am trying to sneak something in the back door simply to prove it can be done. I tried installing DBI in my own directory (This is *NIX) but it was a no go b/c I don't even have access to compilers, make, etc., etc. The reason for wanting it all in one file is for "perception of ease" on the part of some of those who are anti my idea. If I can just drop in one file, show what we need done getting done, then that proves the point soundly. The reason for the packges is that there will be different version of the SQL for different situations, which could be differentiated by calling $mod1::sql vs $mod2::sql. I would knee-jerk hate all this too, but I'm backed in a corner...

Replies are listed 'Best First'.
Re: packages / modules in the same file
by dragonchild (Archbishop) on Nov 13, 2001 at 19:41 UTC
    You can defininitely have modules all in the same file. You can have modules across multiple files. You're just missing a few key ideas.

    The first is that you don't need Exporter if you're doing OO or are in the same file. Exporter is if you want to make a repository of functions, then bring some of them into another file. If you're all in the same file and package, then everything has access to the right symbols.

    If you're doing OO, then just do a PACKAGE->new just like normal. use is only to compile another file which has a certain naming convention, and bring it into your namespace. If you have a class in your file, just work with it as if it had been in another file and you'd use'd it.

    The third is that you're not going back to the main package. You need to tell the interpreter where the first line to execute is. That's the first line in the main package. Try the following:

    package Foo; sub new { print "In Foo::new\n"; my $self = bless {}, 'Foo'; return $self; } package main; my $bar = Foo->new; print "$bar\n";
    That should work just fine.

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      wow, how silly my mistake was. I was forgetting to put in the package main; at the end of the "module". I guess I was thinking the 1; would automagically put me back in main, but I don't know why...

      I agree that the OO syntax is cleaner and wanted to use it. Both OO and non suffered from my oversight, though. anyway, here's what I am going with:

      #!/usr/bin/perl -w use strict; package try; sub new { my $self = {}; $self->{SQL} = 'select name from v$database;'; bless $self, 'try'; return $self; } package main; my $getit = try->new; system("echo \'$getit->{SQL}\' | sqlplus -s system/manager");
      Thanks =]

      We speak the way we breathe. --Fugazi

(tye)Re: packages / modules in the same file
by tye (Sage) on Nov 13, 2001 at 22:02 UTC

    A minor point that probably doesn't matter anymore for this particular project, but that knowing about might save you some time later: can't see your lexical variables and so can't export them for you. You'd need to replace "my" with "our" or, better, "use vars" if you wanted to do that.

    You can export a lexical by writing your own import() method, but you'd have to export it to a global in the calling package so you can't completely avoid globals when exporting.

    Note that you can do something like:

    my $var; use My::Package( Import=>\$var ); # or even use ACME::Widget( Import => \my( $widget ) );
    to get around that.

            - tye (but my friends call me "Tye")
Re: packages / modules in the same file
by clintp (Curate) on Nov 13, 2001 at 19:49 UTC
    As a matter of fact, I posted a node yesterday that has a "module" in the same file as a working program. It's OO so there's no Exporter crap. Uh.. Crap. I can't figure out how to link to the node in Perlmonks. Too much on my brain today.

    Go to the Code section, and it's called "Perl/Tk Space Invaders Sprite Class"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://125061]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-08-22 19:50 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (340 votes). Check out past polls.