Notice that you don't HAVE to use namespaces with require or use. All of that stuff is just conventions (though the import that can be implicit in use will be to what the package name seems to be). And it's usually nicer to use "use" than "require" or "do" even for "include", since you will see any errors before runtime of the main program gets there, subs in there are already known to the compiler when it's compiling your main code, and there is some implicit checking that the "included" code didn't crash.

So it's perfectly ok to e.g. have a Foo.pm file containing:

# Warnings, strict and our aren't needed if you don't # want to bother. I Just like to have them use strict; use warnings; our $c = 5; sub foo { print $c; } # Next line is the only thing that's different from # what you'd have if you were going to use "do" # A filed being required/used should return something true 1;

And then use that as:

use Foo(); use strict; # you couldn't call foo without () under use strict # if you had used "require" or "do" foo; our $c; print $c;

Notice the () in use Foo(). It avoids a call to import, though supposedly if you use this you won't have a Foo package and there will be no import there, so you could leave them out too.

Having said that, it's still often a good idea to make commonly used code into a real module. You can use the Exporter if you want subs and variables in there available unqualified in your main code. It's only a few trivial lines more:

package Foo; # This line is new use strict; use warnings; use base qw(Exporter); # This line is new our @EXPORT_OK =qw($c foo); # This line is new, stuff the caller MAY +import to use it unqualified our $c = 5; sub foo { print $c; } 1;
And on use you have to say which of the exported things you'd like to use:
# Next line is different, explicitely say we want to use foo unqualifi +ed (without Foo::) use Foo qw(foo); use strict; foo; # We could also have listed $c in the import list and then use plain $ +c # But let's instead show you can still access it by qualifying print $Foo::c;

In reply to Re: I just want to include another file by thospel
in thread I just want to include another file by Cap'n Steve

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.