The principle is to do something first before calling a method, such as checking the parameter or something. This keyword is also available in the dancer. When the request arrives, it can pass the check
The remote IP address is used to determine whether the request is an illegal IP address.
#! /Usr/bin/perl
# File: before. pl
# Date: 2014/08/26
# Author: Darkstar
# Use moose to define a class
Package person;
Use moose;
Has 'name' => (
Is => 'ro ',
ISA => 'str ',
Required => 1,
);
Has 'age' => (
Is => 'rw ',
ISA => 'int ',
Required => 1,
);
# Check whether the age is 18 before drinking. The logic of the age check can be stored in drink_brandy.
# Demonstrate before Functions
Before 'Drink _ brandy '=> sub {
My $ self = shift;
If ($ self-> age> 18 ){
Print "you can drink brandy! \ N"
}
Else {
Print "Get Out Boy! \ N ";
Exit;
}
};
Sub drink_brandy {
Print "Good! \ N ";
};
1;
Use person;
# Generate a person object, aged 16
My $ p1 = person-> New (name => 'dark', age => 16 );
Print $ P1-> name, "\ n ";
Print $ P1-> age, "\ n ";
$ P1-> drink_brandy; # Drinking directly. Before checks the age for us. If it is less than 18, the program will be interrupted.
# Program output:
Dark
16
Get Out Boy! # Go home for a child!
Perl moose Modifier