Perl Example Fine Solution Learning notes First day

Source: Internet
Author: User
Tags variables stdin learn perl

One, example one

#!/usr/bin/perl     
print "What ' s your name?";     
Chomp ($name = <STDIN>);     
Print "Welcome, $name, are you ready learn Perl now?"     
Chomp ($response = <STDIN>);     
if ($response eq "yes" or $response eq "y") {     
  print "great! Let's get started learning Perl by example.\n ";     
}     
else {     
 print "o.k. Try again later.\n" 
}     
$now = localtime;     
Print "$name, ran this script on $now. \ n";

Explain

01 lines are labeled Interpreter is Perl execution

02 Print to screen output with print, Perl "Display content" with print;

03chomp remove the last character, that is, remove the line feed

$name = <STDIN> is to set a variable $name from the standard input <stdin>

Note that the variables in Perl are the $ start

Note that Perl variables can be used without a statement, very flexible

Run as follows

04 and 05.

06 is an If statement to judge, the format is

if (condition 1 is established) {    
EXECUTE statement 1    
}    
else {    
EXECUTE statement 2    
}

$response eq "yes" or $response eq "y" means that the value of the variable $response is yes or Y

12 is to set a variable now, from Perl's built-in functions localtime

13 is the display

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.