2020年6月9日火曜日

開発環境

初めてのPerl 第7版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)近藤 嘉雪(翻訳)嶋田 健志(翻訳)、オライリージャパン)の15章(プロセス管理)、15.9(練習問題)2の解答を求めてみる。

コード

#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;

say "2.";

my $dir = shift @ARGV;

open STDOUT, '>', 'ls.out' or die $!;
open STDERR, '>', 'ls.err', or die $!;

chdir $dir or die "can't chdir $dir: $!";
exec 'ls', '-l';

入出力結果(Zsh、PowerShell、Terminal)

% ./sample2.pl 
2.
% cat ls.out 
% cat ls.err 
Use of uninitialized value $dir in chdir at ./sample2.pl line 13.
Use of uninitialized value $dir in concatenation (.) or string at ./sample2.pl line 13.
can't chdir : No such file or directory at ./sample2.pl line 13.
% ./sample2.pl /
2.
% cat ls.out 
total 10
drwxrwxr-x+ 43 root  admin  1376  6  8 05:27 Applications
drwxr-xr-x  67 root  wheel  2144  4  9 17:09 Library
drwxr-xr-x@  8 root  wheel   256  9 30  2019 System
drwxr-xr-x   5 root  admin   160  9 30  2019 Users
drwxr-xr-x   6 root  wheel   192  6  9 14:55 Volumes
drwxr-xr-x@ 38 root  wheel  1216  4  2 02:18 bin
drwxr-xr-x   3 root  wheel    96 10  2  2019 com.apple.TimeMachine.localsnapshots
drwxr-xr-x   2 root  wheel    64  8 25  2019 cores
dr-xr-xr-x   3 root  wheel  4978  5  3 17:19 dev
lrwxr-xr-x@  1 root  admin    11 10  8  2019 etc -> private/etc
lrwxr-xr-x   1 root  wheel    25  5  3 17:19 home -> /System/Volumes/Data/home
drwxr-xr-x   3 root  wheel    96 10  8  2019 opt
drwxr-xr-x   6 root  wheel   192  4  9 17:08 private
drwxr-xr-x@ 63 root  wheel  2016  4  2 02:18 sbin
lrwxr-xr-x@  1 root  admin    11 10  8  2019 tmp -> private/tmp
drwxr-xr-x@ 11 root  wheel   352 10  8  2019 usr
lrwxr-xr-x@  1 root  admin    11 10  8  2019 var -> private/var
% cat ls.err 
% ./sample2.pl abcde
2.
% cat ls.out 
% cat ls.err 
can't chdir abcde: No such file or directory at ./sample2.pl line 13.
% 

0 コメント:

コメントを投稿