Perl moose Modifier

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.