Skip to Content
『Learning Perl, 8th Edition』
book

『Learning Perl, 8th Edition』

by Randal L. Schwartz, brian d foy, Tom Phoenix
March 2025
Beginner to intermediate
398 pages
5h 33m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Perl, 8th Edition』

付録 A. 練習問題の解答

この付録には、本書中に登場する練習問題の解答がコンテナで収められている。

第1章 練習問題の解答

  1. この練習は簡単だ。 我々はすでに君たちにプログラミングを提供したのだから。 君たちの仕事はそれを機能させることだ:

    print "Hello, world!\n";

    v5.10以降であれば、say :

    use v5.10;
    say "Hello, world!";

    ファイルを作成せずにコマンドラインから試したい場合は、-eスイッチを使ってコマンドラインでプログラミングを指定することができる:

    $ perl -e 'print "Hello, World\n"'

    -l というスイッチもあり、これは自動的に改行を追加してくれる:

    $ perl -le 'print "Hello, World"'

    Windowsのcommand.exe(またはcmd.exe)の引用符付けは、外側に二重引用符が必要なので、それを切り替える:

    C:\> perl -le "print 'Hello, World'"

    シェルのクォート内で一般化クォートを使えば、クォートで頭を悩ませずに済む:

    C:\> perl -le "print q(Hello, World)"

    v5.10以降では、-E スイッチを使って新機能を有効にすることができる。これにより、say :

    $ perl -E 'say q(Hello, World)'

    コマンドラインでこれを試すとは思っていなかった。しかし、これも一つの方法だ。コマンドライン・スイッチと機能の完全なリストはperlrunを参照のこと。

  2. perldocコマンドはPerlに付属しているはずなので、直接実行できるはずだ。perldocが発見できない場合は、システムに別のパッケージをインストールする必要があるかもしれない。例えばUbuntuではperl-doc 。

  3. このプログラミングも、前のエクササイズさえうまくいけば簡単だ:

    @lines = `perldoc -u -f atan2`;
    foreach (@lines) {
      s/\w<(.+?)>/\U$1/g;
      print;
    }

第2章 練習問題の解答

  1. ここに一つの方法がある:

    #!/usr/bin/perl
    use warnings;
    $pi = 3.141592654;
    $circ = 2 * $pi * 12.5;
    print "The circumference of a circle of radius 12.5 is $circ.\n";

    ご覧のように、このプログラムは典型的な#! 。Perlへのパスは異なるかもしれない。また、警告をオンにした。

    最初のコード行では、$pi の値をπの値にセットしている。優れたプログラマがこのように定数値を使うことを好む理由はいくつかある。3.141592654 が複数回必要になった場合、プログラムに入力するのに時間がかかる。ある場所で誤って3.141592654 、別の場所で誤って3.14159 、数学的バグかもしれない。誤って3.141952654 と入力して、誤った惑星に宇宙探査機を送ってしまわないようにチェックする行が1行しかない。

    最近のPerlでは、よりファンシーな文字を変数名として使うことができる。ソースコードにUnicode文字が含まれていることをPerlに伝えれば、π文字を名前付けに使うこともできた(付録C参照):

    #!/usr/bin/perl use utf8; use warnings; $π = 3.141592654; $circ = 2 * $π ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 9798341636231