How do I use constants from a Perl module?

前端 未结 6 1739
情书的邮戳
情书的邮戳 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 12:10

    To add to the bag of tricks, since a constant is just a subroutine you can even call it as a class method.

    package Foo;
    use constant PI => 3.14;
    
    print Foo->PI;
    

    If you have lots of constants it's a nice way to get at the occasional one without having to export them all. However, unlike Foo::PI or exporting PI, Perl will not compile out Foo->PI so you incur the cost of a method call (which probably doesn't matter).

提交回复
热议问题