Ubuntu 中的编程语言(中)

Perl

Perl 语言在2010年6月编程语言排行榜中排名第八位。下面就是 GregorianTest.pl 程序:

|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | use Time::Piece; use Time::Local; use Time::Seconds; my $dt = ``localtime``(timelocal(0, 0, 0, 4, 10 - 1, 1582)); print $dt``.``"\n"``; $dt += ONE_DAY; print $dt``.``"\n"``; |

Ubuntu 操作系统中已经预装了 Perl。解释执行:

复制代码
ben@ben-1520:~/work$ perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
ben@ben-1520:~/work$ perl GregorianTest.pl
Mon Oct  4 00:00:00 1582
Tue Oct  5 00:00:00 1582
ben@ben-1520:~/work$ 

运行结果和 .NET 平台的编程语言一样。

此外,还可以使用 cpan 命令来安装 DateTime 模块:

复制代码
ben@ben-1520:~/work$ sudo cpan
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.
cpan[1]> install DateTime
cpan[2]> m DateTime
Module id = DateTime
    DESCRIPTION  A complete, easy to use date and time object
    CPAN_USERID  DROLSKY (Dave Rolsky )
    CPAN_VERSION 0.55
    CPAN_FILE    D/DR/DROLSKY/DateTime-0.55.tar.gz
    UPLOAD_DATE  2010-03-16
    DSLIP_STATUS bmpOp (beta,mailing-list,perl,object-oriented,Standard-Perl)
    MANPAGE      DateTime - A date and time object
    INST_FILE    /usr/local/lib/perl/5.10.1/DateTime.pm
    INST_VERSION 0.55
cpan[3]> q
Terminal does not support GetHistory.
Lockfile removed.
ben@ben-1520:~/work$ 

下面就是使用 DateTime 模块的 GregorianTest2.pl 程序:

|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | use DateTime; my $dt = DateTime->new(``year``=>1582, ``month``=>10, ``day``=>4); print $dt``->day_abbr.``" "``.``$dt``->ymd.``"\n"``; $dt``->add(``days``=>1); print $dt``->day_abbr.``" "``.``$dt``->ymd.``"\n"``; |

解释执行:

复制代码
ben@ben-1520:~/work$ perl GregorianTest2.pl
Mon 1582-10-04
Tue 1582-10-05
ben@ben-1520:~/work$ 

运行结果还是和仅使用 Perl 核心模块的程序一样,没有什么改善。

PHP

PHP 语言在2010年6月编程语言排行榜中排名第四位。下面就是 GregorianTest.php 程序:

|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | <?php $date = ``new DateTime(); echo $date``->getTimezone()->getName().``"\n"``; echo $date``->format(``'D Y-m-d'``).``"\n"``; $date``->setDate(1582, 10, 4); echo $date``->format(``'D Y-m-d'``).``"\n"``; $date``->add(``new DateInterval(``'P1D'``)); echo $date``->format(``'D Y-m-d'``).``"\n"``; ?> |

安装 PHP 客户端工具,可以作为交互窗口(使用 --interactive 或者 -a 参数),也可以解释执行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install php5-cli
ben@ben-1520:~/work$ php -v
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
ben@ben-1520:~/work$ php -a
Interactive shell
php > echo "PHP ".phpversion();
PHP 5.3.2-1ubuntu4.2
php > exit;
ben@ben-1520:~/work$ php GregorianTest.php
Asia/Chongqing
Wed 2010-06-16
Tue 1582-10-04
Wed 1582-10-05
ben@ben-1520:~/work$ 

非常奇怪,PHP 语言的 DateTime 类居然认为1582年10月4日是星期二,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!

此外,PHP 语言还有和历法相关的函数。下面就是 GregorianTest2.php 程序:

|-------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php function writeline(``$cal``, ``$jd``) { ``$dt = cal_from_jd(``$jd``, ``$cal``); ``$info = cal_info(``$cal``); ``echo $dt``[``"abbrevdayname"``].``" "``.``$dt``[``"date"``]; ``echo "\tdays from 4713-01-01 B.C.: "``.``$jd``; ``echo "\t"``.``$info``[``"calname"``]; ``echo "\n"``; } $jd = cal_to_jd(CAL_JULIAN, 10, 4, 1582); writeline(CAL_JULIAN , 0); writeline(CAL_JULIAN , 1); writeline(CAL_JULIAN , ``$jd``); writeline(CAL_GREGORIAN, ``$jd + 1); writeline(CAL_JULIAN , ``$jd + 1); ?> |

解释执行:

复制代码
ben@ben-1520:~/work$ php GregorianTest2.php
Mon 0/0/0	days from 4713-01-01 B.C.: 0	Julian
Tue 1/2/-4713	days from 4713-01-01 B.C.: 1	Julian
Thu 10/4/1582	days from 4713-01-01 B.C.: 2299160	Julian
Fri 10/15/1582	days from 4713-01-01 B.C.: 2299161	Gregorian
Fri 10/5/1582	days from 4713-01-01 B.C.: 2299161	Julian
ben@ben-1520:~/work$ 

注意,在 GregorianTest2.php 程序中必须由用户自己指定使用儒略历还是格里历。

PHP 语言主要应用是服务端,是 LAMP (Linux + Apache + MySQL + PHP, or Perl, or Python) 的重要组成部分,用于架设动态网站。

Pascal

Pascal 语言在2010年6月编程语言排行榜中排名第十五位。下面就是 GregorianTest.pas 程序:

|----------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | Program GregorianTest(output); type ``StringArray = ``array``[``0..6``] ``of string``[``3``]; var ``t: TimeStamp; ``a: StringArray; procedure Init; begin ``a[``0``] := "Sun"; ``a[``1``] := "Mon"; ``a[``2``] := "Tue"; ``a[``3``] := "Wed"; ``a[``4``] := "Thu"; ``a[``5``] := "Fri"; ``a[``6``] := "Sat"; end``; begin ``Init; ``GetTimeStamp(t); ``WriteLn``(a[t``.``DayOfWeek], ``' '``, Date(t)); ``t``.``Year := ``1582``; ``t``.``Month := ``10``; ``t``.``Day := ``4``; ``t``.``DayOfWeek := ``4``; ``WriteLn``(a[t``.``DayOfWeek], ``' '``, Date(t)); end``. |

安装 GNU Pascal,编译和运行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install gpc
ben@ben-1520:~/work$ gpc --version
gpc 20070904, based on gcc-4.1.3 20080704 (prerelease) (Ubuntu 2.1-4.1.2-27ubuntu2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ben@ben-1520:~/work$ gpc -o GregorianTest GregorianTest.pas
ben@ben-1520:~/work$ ./GregorianTest
Wed 16 Jun 2010
Thu  4 Oct 1582
ben@ben-1520:~/work$ 

在 GNU Pascal 中,我没有找到计算某一日期是星期几的函数,也没有找到计算某一日期的下一天的函数。程序之所以能够正确输出星期四,是由于在 GregorianTest.pas 程序的第24行对 DayOfWeek 字段进行了赋值。

Delphi

Delphi 语言在2010年6月编程语言排行榜中排名第十位。下面就是 GregorianTest2.pas 程序:

|-------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Program GregorianTest2; Uses sysutils; var ``t: TTimeStamp; procedure WriteLine(t: TTimeStamp); begin ``Write``(FormatDateTime(``'ddd yyyy-mm-dd'``, TimeStampToDateTime(t))); ``Writeln``(``' days past 0001-01-01: '``, t``.``Date); end``; Begin ``t := DateTimeToTimeStamp(EnCodeDate(``1``, ``1``, ``1``)); ``WriteLine(t); ``t := DateTimeToTimeStamp(EnCodeDate(``1582``, ``10``, ``4``)); ``WriteLine(t); ``t``.``Date := t``.``Date + ``1``; ``WriteLine(t); ``t := DateTimeToTimeStamp(Now); ``WriteLine(t); ``t``.``Date := t``.``Date + ``1``; ``WriteLine(t); End``. |

安装兼容 Delphi 的 Free Pascal,编译和运行:

复制代码
ben@ben-1520:~/work$ sudo apt-get install fp-compiler
ben@ben-1520:~/work$ fpc GregorianTest2.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling GregorianTest2.pas
Linking GregorianTest2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
22 lines compiled, 0.4 sec 
ben@ben-1520:~/work$ ./GregorianTest2
Sat 0001-01-01  days past 0001-01-01: 1
Sat 1582-10-04  days past 0001-01-01: 577725
Fri 1582-10-05  days past 0001-01-01: 577726
Wed 2010-06-16  days past 0001-01-01: 733939
Thu 2010-06-17  days past 0001-01-01: 733940
ben@ben-1520:~/work$ 
相关推荐
墨家句子4 分钟前
AnythingLLM 搭本地知识库:文档问答不准怎么调
linux·后端
高山有多高4 分钟前
【Linux笔记】冯诺依曼体系结构
linux·运维·笔记
筑梦之路22 分钟前
Alibaba Cloud Linux 4 IPXE全自动安装(直连官方源)——筑梦之路
linux·运维·服务器·国产化·阿里云操作系统
dadaobusi23 分钟前
学习:chiesel,scala
学习·fpga开发·scala
encoderlee31 分钟前
ubuntu server 如何播放视频显示网页
linux·ubuntu·音视频·树莓派·ubuntu server·arm嵌入式
云计算练习生38 分钟前
什么是 Linux?从 Unix 到开源生态的完整脉络
linux·开源·操作系统·unix
Pointer Pursuit41 分钟前
make / Makefile
linux·运维·服务器
ZhangJun9542 分钟前
Linux常用命令笔记,中高级运维高频指令,并附上vim常见操作
linux·运维·centos·云计算·vim
FPGA小徐1 小时前
Ubuntu 22.04 安装 Miniconda、PyTorch 与 YOLOv8 并完成 CPU 推理
pytorch·yolo·ubuntu
逐流人1 小时前
Ceph对象存储与文件系统存储管理:从RADOS网关、S3与Swift到CephFS快照同步
linux·运维·ceph·云原生·云计算·swift·rados