如何使用 perl 文件添加版权行以写入示例 perl 文件

How to add a copyright line using a perl file to write into a sample perl file

所以我不得不写一个Perl文件,将一行版权信息添加到另一个Perl文件。

我有一个名为 "copyright.pl" 的文件,我希望它写入一个名为 "exercise.pl".

的文件

这是copyright.pl:

$^I = ".bak"; # make backups
while (<>) {
     if (/\A#!/) { # is it the shebang line?
     $_ .= "## Copyright (C) 2020 by Rashter\n";
     }
print;
}

这是exercise.pl:

 print "This is an exercise file!";

我应该如何处理 运行 我的版权文件,以便它可以在 exercise.pl 添加版权行?我什么都试过了...

最简单的解决方案之一是使用特殊变量 $..

在代码的第一行之后插入许可证,据推测应该是 'shebang'

请查看以下代码是否产生了预期的结果

use strict;
use warnings;
use feature 'say';

$^I = ".bak";

my $licence = '
# This a licence agreement about the code present in this file
# 
# Copyright (C) 2020 by Owner <owner@inter.net>
#
# Text of licence agreement....
# ....
# ....
# End of licence agreement
';

while(<>) {
    chomp;
    say $licence if $. == 2;
    say;
}