How do you create a Perl module?

后端 未结 8 1845
醉话见心
醉话见心 2020-12-13 21:03

How do you write a module for Perl? In Python you can use:

# module.py
def helloworld(name):
    print \"Hello, %s\" % name

# main.py
import module
module.         


        
8条回答
  •  不思量自难忘°
    2020-12-13 21:25

    The most traditional way of setting up a module is as follows:

    package Foo::Bar;
    our @ISA       = qw(Exporter);       # Tells perl what to do with...
    our @EXPORT    = qw(sub1 sub2 sub3); # automatically exported subs
    our @EXPORT_OK = qw(sub4 sub5);      # exported only when demanded
    
    # code for subs, constants, package variables here
    
    1;  # Doesn't actually have to be 1, just a 'true' value.
    

    and as others have said, you can use it like so:

    use Foo::Bar;
    

提交回复
热议问题