開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.28 (プログラミング言語)
初めてのPerl 第7版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の9章(正規表現によるテキスト処理)、9.6(練習問題)5の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '5.';
my %files;
for (@ARGV) {
$files{$_} = 1;
}
while (<>) {
if (/\A## Copyright/) {
delete $files{$ARGV};
}
}
@ARGV = keys %files;
$^I = ".bak";
while (<>) {
if (/\A#!/) {
$_ .= '## Copyright (C) 2020 by kamimura\n'
}
print;
}
入出力結果(Zsh、PowerShell、Terminal)
% ls dir5
./ sample1.pl* sample3.pl* sample5.pl*
../ sample2.pl* sample4.pl*
% head dir5/*
==> dir5/sample1.pl <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '1.';
my $what = shift @ARGV;
while (<>) {
==> dir5/sample2.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
use warnings;
use v5.28;
say '2.';
my $filename = shift @ARGV;
==> dir5/sample3.pl <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '3.';
my $filename = shift @ARGV;
open my $in_fh, '<', $filename or die $!;
==> dir5/sample4.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
use warnings;
use v5.28;
say '4.';
$^I = '.bak';
==> dir5/sample5.pl <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '5.';
my %files;
for (@ARGV) {
$files{$_} = 1;
% ./sample5.pl dir5/*
5.
% ls dir5
./ sample1.pl* sample2.pl* sample3.pl.bak* sample5.pl*
../ sample1.pl.bak* sample3.pl* sample4.pl* sample5.pl.bak*
% head dir5/*.pl
==> dir5/sample1.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura\nuse strict;
use warnings;
use v5.28;
say '1.';
my $what = shift @ARGV;
while (<>) {
==> dir5/sample2.pl <==
#!/usr/bin/env perl
## Copyright ©️ 2020 by kamimura
use strict;
use warnings;
use v5.28;
say '2.';
my $filename = shift @ARGV;
==> dir5/sample3.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura\nuse strict;
use warnings;
use v5.28;
say '3.';
my $filename = shift @ARGV;
open my $in_fh, '<', $filename or die $!;
==> dir5/sample4.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura
use strict;
use warnings;
use v5.28;
say '4.';
$^I = '.bak';
==> dir5/sample5.pl <==
#!/usr/bin/env perl
## Copyright (C) 2020 by kamimura\nuse strict;
use warnings;
use v5.28;
say '5.';
my %files;
for (@ARGV) {
$files{$_} = 1;
% head dir5/*.bak
==> dir5/sample1.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '1.';
my $what = shift @ARGV;
while (<>) {
==> dir5/sample3.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '3.';
my $filename = shift @ARGV;
open my $in_fh, '<', $filename or die $!;
==> dir5/sample5.pl.bak <==
#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
say '5.';
my %files;
for (@ARGV) {
$files{$_} = 1;
%
0 コメント:
コメントを投稿