Guide Practice of Perl

#!/usr/bin/perl
use strict;
use warnings;

#This is a practice of perl, as which shows with below perl code...codes.

print "Hello, world!\n";

my $animal = "camel\n";
my $answer = 42;

printf $animal;
print "The animal is $animal\n";
print "The square of $answer is ", $answer * $answer, "\n";

print;

my @test = ("t1", "t4", "t3");
print $test[0], $test[1], $test[$#test], "\n";
print "@test\n";

my @sorted = sort @test;
print "@sorted\n";

my @backwards = reverse @test;
print "@backwards\n";

my %fcolor = (
  apple => "red",
  banana => "yellow",
);

my $apple = $fcolor{"apple"};
print "apple color is $apple. \n";

my @fruits = keys %fcolor;
my @colors = values %fcolor;
print "list fruits: @fruits. \n";
print "list colors: @colors. \n";

my $variables = {
  scalar => {
    desc => "sigle item",
    sigil => '$',
  },

  array => {
    desc => "ordered list of items",
    sigil => '@',
  },

  hash => {
    desc => "key/value pairs",
    sigil => '%',
  },

};
print "scalar begin with a $variables->{'scalar'}->{'sigil'}. \n";
print "array begin with a $variables->{'array'}->{'sigil'}. \n";
print "hash begin with a $variables->{'hash'}->{'sigil'}. \n";

my $x = "x";
my $some_condition = 1;
if ($some_condition) {
  my $y = "y";
  print $x;
  print $y;
}
print $x;
#print $y;
print "\n";

print "unless condition.\n" unless(0);

until (1) {
  print "while condition.\n";
};

foreach (@test) {
  print "array element is $_\n";
}

print "array element is $test[$_]\n" foreach 0 .. $#test;

foreach my $key (keys %fcolor) {
  print "The value of $key is $fcolor{$key} \n";
}

my $a = 1;
$a += 1;
print "$a\n";
$a -= 1;
print "$a\n";
$a .= "x";
print "$a\n";

open(my $in, "<", "input.txt") or die "can't open input.txt: $!\n";

#my $line = <$in>;
#my @lines = <$in>;
#print "$line";
#print "@lines";

print stderr "stderr test.\n";

while (<$in>) {
  print "$_";
}
print "\n";

close $in or die "$in: $!";

open(my $out, ">", "output.txt") or die "can not open output.txt. \n";
print $out "hello world!\nWrite this info into output.txt. \n";
close $out or die "$out: $!";

=pod
while(<>) {
    next if /^$/;
    last if (/q/);
  print;
}
=cut

my $email = "hk.mars\@aol.com";

if ($email =~ /([^@]+)@(.+)/) {
  print "username is $1 \n";
  print "hostname is $2 \n";
}

#subroutines
sub loger {
  my $logmessage = shift;

  open my $logfile, ">>", "my.log" or die "can not open my.log.$!";
  print $logfile $logmessage;
}

loger("hello loger! \n");

sub square {
  my $num = shift;
  my $sq;

  $sq = $num * $num;
  return $sq;
}

my $sq = square(5);
print "the square result of 5 is $sq \n";

#OO Perl

#Using Perl modules

#The end of this practice, let's start to write project...now...

Write above perl codes into "e1.pl" file or any name as you want, and executes it on the shell, below is the result shown:

Yet, perl is so easy as C. I am so expected to start  the web project using perl. I guess only need one week to hack a website done.

Mars

时间: 2024-08-28 14:39:50

Guide Practice of Perl的相关文章

Shell Style Guide

Shell Style Guide Revision 1.26 Paul ArmstrongToo many more to mention Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: ▽. You may toggle all summaries with th

Spark1.1.0 Spark Programming Guide

Spark Programming Guide Overview Linking with Spark Initializing Spark Using the Shell Resilient Distributed Datasets (RDDs) Parallelized Collections External Datasets RDD Operations Basics Passing Functions to Spark Working with Key-Value Pairs Tran

ZooKeeper Administrator&#39;s Guide A Guide to Deployment and Administration(吃别人嚼过的馍没意思,直接看官网资料)

Deployment System Requirements Supported Platforms Required Software Clustered (Multi-Server) Setup Single Server and Developer Setup Administration Designing a ZooKeeper Deployment Cross Machine Requirements Single Machine Requirements Provisioning

Linux Kernel - Debug Guide (Linux内核调试指南 )

http://blog.csdn.net/blizmax6/article/details/6747601 linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 建立调试环境 发行版的选择和安装 安装交叉编译工具 bin工具集的使用 qemu的使用 initrd.img的原理与制作 x86虚拟调试环境的建立 arm虚拟调试环境的建立 arm开发板调试环

A Full Hardware Guide to Deep Learning

A Full Hardware Guide to Deep Learning Deep Learning is very computationally intensive, so you will need a fast CPU with many cores, right? Or is it maybe wasteful to buy a fast CPU? One of the worst things you can do when building a deep learning sy

(转) A Survival Guide to a PhD

A Survival Guide to a PhD Sep 7, 2016 This guide is patterned after my “Doing well in your courses”, a post I wrote a long time ago on some of the tips/tricks I’ve developed during my undergrad. I’ve received nice comments about that guide, so in the

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

Method Calling in Perl

This is notes from reading the "object oriented perl programming". Elements of the @_ array are special in that they are not copies of the actual arguments of the function call. Rather they are aliases for those arguments. That means that if val

神经网络指南Hacker&#39;s guide to Neural Networks

Hi there, I'm a CS PhD student at Stanford. I've worked on Deep Learning for a few years as part of my research and among several of my related pet projects is ConvNetJS - a Javascript library for training Neural Networks. Javascript allows one to ni