Note: Functions and methods with _
at the beginning are private, so it is not good to access them from the outside. (Pycharm gives a warning)
Foo.pm
package Foo;
use v5.10;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw/pub_func/; # pub_Allow func functions to be exported
sub pub_func {
say "pub_func";
}
sub _pri_func {
say "_pri_func";
}
1;
main.pl
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin";
use Foo qw/pub_func/; # pub_Import the func function into the namespace
sub main {
pub_func(); # pub_func
Foo::pub_func(); # pub_func
Foo::_pri_func(); # _pri_func
}
if ($0 eq __FILE__) {
main();
}
foo.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division, absolute_import
def pub_func():
print("pub_func")
def _pri_func():
print("_pri_func")
main.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division, absolute_import
import foo
from foo import pub_func # pub_Import the func function into the namespace
def main():
pub_func() # pub_func
foo.pub_func() # pub_func
foo._pri_func() # _pri_func
#Pycharm in my head_Functions with"Access to a protected member _pri_func of a module"Will give you a warning
if __name__ == '__main__':
main()
For how to write from module import name1, name2
We do not recommend it because it may import multiple same function names into the namespace as follows.
Python good idioms, bad idioms
This time, it's a much weaker "no good" than the previous "no good", but it's still better to stop it unless there is a good reason. This usually doesn't work because you end up having one object that lives in two separate namespaces before you know it.
main2.pl
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin";
#use Foo qw/pub_func/;
use Foo;
sub pub_func {
return Foo::pub_func(@_);
}
sub main {
pub_func(); # pub_func
Foo::pub_func(); # pub_func
Foo::_pri_func(); # _pri_func
}
if ($0 eq __FILE__) {
main();
}
By doing this, the arguments and return values can be thrown in full, so the function name can be changed freely. I didn't know how to throw all the arguments in the Python version, so I omitted it. (I wonder if I have to write everything properly)
Recommended Posts