Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

function encapsulation

by Buckaroo Buddha (Scribe)
on Jul 13, 2000 at 18:11 UTC ( #22381=perlquestion: print w/replies, xml ) Need Help??

Buckaroo Buddha has asked for the wisdom of the Perl Monks concerning the following question:

the Magic Box system? (use require) was a question i
posted a while ago about saving a complete function
to a file so it could be used but not have to be seen

there are some advantages to this (as i see it) they may be
advantages specific to my (chaotic) programming style or it might
help you too. (navigating only the section that you're
working on, not being able to ruin something that works,
and many others)

anyways, one of the mailing lists i'm on had the answer
i was looking for ... so for those of you not, on the
mailing list, but intersted (if it's anyone other than me)
here's the answer :)
Q:

I am a C/C++ programmer and new to Perl.  I created severl sub in one file. 
Can anyone tell me how to call these sub from another file? Because I am
new,
I don't want to make pm module which sounds complicated at this stage of my
learning.
Thank you

A:
 Just say "USE" that file where you have written the function and you can
call it,
 like 
 use filename;
 filename::functionname(parameters)

Replies are listed 'Best First'.
Re: function encapsulation
by ZZamboni (Curate) on Jul 13, 2000 at 18:56 UTC
    A few points:
    • If you use use, the file has to have a .pm extension, even if it does not have any package definitions. On the other hand, require can take any arbitrary filename as an argument, so you could do:
      require 'file.pl';
      if you want, whereas with use the file needs to be called "file.pm".
    • In both cases (use and require), you do not need the filename::function syntax you mentioned if the file does not contain any package declarations and just has the function definitions. The functions are imported into your current namespace.
    • In both cases the included file has to return a true value. This is usually accomplished by putting a line containing "1;" at the end of the file.

    --ZZamboni

      more points:
      • 'use' happens when the script is compiled
      • 'require' happens as the script runs
      So, if you make any changes to @INC, it won't effect any of the 'use' statements, but it will the 'require's.
      Also, if you 'perl -c' the script, the '.pm's that were 'use'd in will be checked for sytax, the ones that were 'require'd in won't be.

      /\/\averick

        That's not to say that you can't make changes to @INC which will affect 'use' statements. Two ways that spring to mind are:

        1. Put the code that changes @INC in a BEGIN block.
        2. Use use lib;
        --
        <http://www.dave.org.uk>

        European Perl Conference - Sept 22/24 2000, ICA, London
        <http://www.yapc.org/Europe/>

        then this raises an interesting question (for me at least)

        i should probably test this myself (and probably will but love
        discussion ... and have a deadline breathing down my neck ;)

        if i make a function, and encapsulate it in a file, then
        make calls to that function from a script which had been
        compiled using perl2exe...
        would the whole program still work?
        could i change the required and encapsulated function as needed?
        is this the basic concept behind a DLL file in windows?
            or are there more to them?

        i've not got a lot comp-sci so i'm trying to pick up advanced topics
        in programming on the fly, while simultaniously learning the basics
        i'd love some help me in gaining a deeper understanding of the whole system
        we're working in

Re: function encapsulation
by jlistf (Monk) on Jul 13, 2000 at 18:24 UTC
    well... there are two ways of doing this (that i can think of, i'm sure there are more). the difference between use and require is that use automatically calls the export (or is it import?) method from the package/module/file that you're using. the way you're calling the function now, either use or require will do what you want.

    if you want to be able to call the function just by typing functionname(parameters) (if you want to import it into your namespace) you have to declare a export function within the module/package/file. the easiest way of doing this is by using the exporter module. just type:
    BEGIN { use Exporter(); @ISA = qw( Exporter ); @EXPORT = qw( &subroutine1 $variable1 ); }
    where &subroutine1 and $variable1 can be any variable and subroutine names.

    update: y'know... its interesting how each person knows different bits of information. i think this thread really highlights that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://22381]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2019-08-21 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?