Perl: Notes for Perl programming.

Source: Internet
Author: User

Here we will summarize the issues that need to be paid attention to when programming in the Perl language.

1. the hash value is not sequential, so the values returned by the hash function are all sort, rather than the status when the hash value is assigned. For example:

my %hash2=( ‘ten‘ => 10, ‘fiirt‘ => 1, ‘second‘ => 2, ‘third‘ => 3, ‘fouth‘ => 4);print keys %hash2;---result-----fiirtsecondthirdfouthten

Using the hash function each, the result is also obtained through sort, for example:

my %hash2=( ‘ten‘ => 10, ‘fiirt‘ => 1, ‘second‘ => 2, ‘third‘ => 3, ‘fouth‘ => 4);while (my ($key, $value) = each %hash2) {    print "$key => $value\n";}-----result------fiirt => 1second => 2third => 3fouth => 4ten => 10

2. There is no difference between Perl variables, array variables, and hash variables and UNDEF. If my (strict requirement) is not used, it cannot be used. The value is UNDEF only if it is defined but not assigned a value. For example, an error occurs in the following example.

if (%dfd) { print ‘ok‘;} else { print ‘not ok‘;}

3. When an undefined value is used as a number, Perl automatically converts it to 0. If you use warnings; use strict. Although the program reports an error, it still produces results.

print 12*$a;------result-------Name "main::a" used only once: possible typo at ts2.pl line 19.Use of uninitialized value $a in multiplication (*) at ts2.pl line 19.0[nan@localhost pl]$ fg

4. <> when reading a file, the UNDEF value is returned after the last row is read, in order to end the loop. For example:

open FILE, "<tt.pl" or die "the file tt.pl can not be opened $!";while(<FILE>) {  print;}

However, when <stdin> reads data from the keyboard, if a while loop is used, it cannot jump out of the loop, so it is best to set a special character to jump out of the loop. For example:

while (<>) {  last if (/^EOF$/);  print;}

 

Perl: Notes for Perl programming.

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.