LOCAL/MY in outsite files in USE

G

Great Deals

I know how to use the sub now. My sub is using my and global
veriables. I want to know how I can put the sub in an outside file. If
I put
in main.pl if i put
use mysub.pl

I know the my in mysub will be fine, but how can the sub in mysub.pl
use the global verible in main.pl?

by the way, it is just copy and paste the whole sub into a seperate
file and just use it in the main file? do i need to package it or
something? any reference on how to pack them? thanks
 
M

Martien Verbruggen

I know how to use the sub now. My sub is using my and global
veriables. I want to know how I can put the sub in an outside file. If

Stop there.

Now continue by reading this:
http://perl.plover.com/FAQs/Namespaces.html

Once you understand that:

- You will need to redesign your interface to use lexicals, or
- You use a package global.

If you want to use a package global, to make it work under strictures,
you need to specify the full name of the variable, use 'our', or if
you're on an old Perl 'use vars'.
I put
in main.pl if i put
use mysub.pl

I know the my in mysub will be fine, but how can the sub in mysub.pl
use the global verible in main.pl?

By referring to it in the correct manner. if you have 'use strict',
which you _should_ have, you need to predeclare you're going to use
it, or use its full name ($main::foo);
by the way, it is just copy and paste the whole sub into a seperate
file and just use it in the main file? do i need to package it or
something? any reference on how to pack them? thanks

$ perldoc perlmod
$ perldoc perlfaq7
/How do I create a module?

In other words, read the perlmod documentation, and the perl faq,
section 7. If you share code, it is generally best to put some work
into doing it nicely, and creating a decent interface to it, instead
of just dumping it in some file somewhere.

Martien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top