How do I use constants from a Perl module?

前端 未结 6 1736
情书的邮戳
情书的邮戳 2020-12-23 11:37

If I define a constant in a Perl module, how do I use that constant in my main program? (Or how do I call that constant in the main program?)

6条回答
  •  误落风尘
    2020-12-23 11:58

    Constants are just subs with empty prototype, so they can be exported like any other sub.

    # file Foo.pm
    package Foo;
    use constant BAR => 123;
    use Exporter qw(import);
    our @EXPORT_OK = qw(BAR);
    
    
    # file main.pl:
    use Foo qw(BAR);
    print BAR;
    

提交回复
热议问题