How do I include functions from another file in my Perl script?

后端 未结 8 1411
陌清茗
陌清茗 2020-11-28 04:46

This seems like a really simple question but somehow my Google-Fu failed me.

What\'s the syntax for including functions from other files in Perl? I\'m looking for s

相关标签:
8条回答
  • 2020-11-28 05:24

    You really should look into perl modules however, for a quick hack you could always run "perl -P" which runs your perl script through the C pre-processor. That means you can do #include and friends....

    Only a quick hack though, beware ;-)

    0 讨论(0)
  • 2020-11-28 05:27

    Use a module. Check out perldoc perlmod and Exporter.

    In file Foo.pm

    package Foo;
    use strict;
    use warnings;
    use Exporter;
    
    our @ISA= qw( Exporter );
    
    # these CAN be exported.
    our @EXPORT_OK = qw( export_me export_me_too );
    
    # these are exported by default.
    our @EXPORT = qw( export_me );
    
    sub export_me {
        # stuff
    }
    
    sub export_me_too {
        # stuff
    }
    
    1;
    

    In your main program:

    use strict;
    use warnings;
    
    use Foo;  # import default list of items.
    
    export_me( 1 );
    

    Or to get both functions:

    use strict;
    use warnings;
    
    use Foo qw( export_me export_me_too );  # import listed items
    
    export_me( 1 );
    export_me_too( 1 );
    

    You can also import package variables, but the practice is strongly discouraged.

    0 讨论(0)
提交回复
热议问题