Perl tag as public or private

Perl does not have public or private access to / subs methods. Therefore, I need to mark some subsystems as safe web calls as simple logic, therefore, in a simple check, if this sub / function is marked as public, then a direct web browser call to this action with the same name as the function is processed otherwise case refused.

Example:

sub Register : public {
 ...... This should be web browser call allowed
}

sub check_login {
  ... this by default should be private and not allowed to browser.
}

I read a few about various modules like attributes () and moose or moo.

What is the best way to achieve this. All I need to do is to check if some subscribers are marked as public or not, will be closed by default.

+4
source share
3 answers

Sub:: Talisman , .

package MyPackage;

use Sub::Talisman qw( Public Private Shiny Author );

sub foo :Public :Author("Bob") {
   ...;
}

sub bar :Private :Shiny :Author("Bob") {
   ...;
}

# Find out if "bar" is private?
my $is_private = grep {
   $_ eq 'MyPackage::Private';
} Sub::Talisman->get_attributes(\&bar);

print "Is bar private? ", ($is_private ? 'YES' : 'NO'), "\n";

# Find out who wrote "foo"?
my ($author) = @{
   Sub::Talisman->get_attribute_parameters(\&bar, 'MyPackage::Author')
};

print "Who wrote foo? ", $author, "\n";
+1

, , .

sub foo_public {
}

sub _bar_private {
}
+2

Sub :: Private uses the syntax you want, although I have no direct experience with the module. In perl, the convention is that private methods begin with underscores, and public does not. However, public / private access does not apply.

+1
source

Source: https://habr.com/ru/post/1526961/


All Articles