Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: packages / modules in the same file

by dragonchild (Archbishop)
on Nov 13, 2001 at 19:41 UTC ( [id://125063]=note: print w/replies, xml ) Need Help??


in reply to packages / modules in the same file

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.

Replies are listed 'Best First'.
(jptxs) Re: packages / modules in the same file
by jptxs (Curate) on Nov 13, 2001 at 20:12 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://125063]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found