Perl Learning Notes (2)

Source: Internet
Author: User



1) Remember just beginning to write Perl, for a function, always take the current data types can be used to solve the problem, do not want to think there is no better, can be used to solve the problem is good, which led to the late, to add functionality inside the function, the function to add a lot of parameters, a function has 7, 8 parameters, And these parameters are all related,



sub is_healthy{
    my ($name, $sex, $age, $height, $weight) = @_;
    
}
like the above to judge whether a person is healthy function, passed these parameters, these parameters are not too much, but, to the later, once you want to add functionality, such as to do some operations according to a person's address, it may be necessary to add another parameter, OK, the parameters of one more, but, The demand may be increasing, plus one more parameter .... The last parameter will be a lot, when calling the function, always see the long tail. That's what I wrote. Finally, after the senior remind, only to find that they really did not think about this problem, why not hash it, how simple and convenient, like this,






My $human = {};
$human->{name} = "1y";


when passing the parameters, just pass one,






Is_healthy ($human);
If you want to add parameters, just add the key value in the hash is good, so in the call function, do not have to do anything to change, easy, but for some irrelevant parameters, have to think about.





2) Perl-E for the interpretation of existence, see the code written by others, use-e to determine whether a directory or file exists,


sub is_healthy{
    my ($name, $sex, $age, $height, $weight) = @_;
    
}
This can determine whether the directory exists, assuming that after the directory exists, the next operation is for the directory operation, but if you accidentally passed a file, and the file was originally for the directory operation, then the future is full of unknown, to be scolded ...


To determine the directory can be used with-D, in this case with-e risk, may be the wrong parameters to the code.







Perl Learning Notes (2)


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.