1. 使用Perl单行命令
基本添加(文件开头):
perl -i.bak -pe 'print "Copyright (c) 2024 Your Company. All rights reserved.\n\n" if $. == 1' *.pl
智能添加(仅当不存在时):
perl -i.bak -pe '
if ($. == 1 && !/^#.*Copyright/) {
print "# Copyright (c) 2024 Your Company\n";
print "# All rights reserved\n\n";
}
print;
' *.pl
2. 完整的Perl脚本
#!/usr/bin/perl
use strict;
use warnings;
my $copyright = <<'COPYRIGHT';
###########################################################################
# Copyright (c) 2024 Your Company Name
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
###########################################################################
COPYRIGHT
my @extensions = qw(.pl .pm .t);
my @files = @ARGV;
foreach my $file (@files) {
# 检查文件扩展名
next unless grep { $file =~ /\Q$_\E$/ } @extensions;
# 读取文件内容
open my $in, '<', $file or warn "Cannot read $file: $!" and next;
my @lines = <$in>;
close $in;
# 检查是否已包含版权信息
my $first_line = $lines[0] // '';
if ($first_line =~ /copyright/i) {
print "$file already has copyright notice. Skipping.\n";
next;
}
# 添加版权信息
open my $out, '>', $file or warn "Cannot write $file: $!" and next;
print $out $copyright;
print $out @lines;
close $out;
print "Added copyright to $file\n";
}
# 如果没有指定文件,处理当前目录
if (!@files) {
find_files('.');
}
sub find_files {
my ($dir) = @_;
opendir my $dh, $dir or die "Cannot open directory $dir: $!";
while (my $entry = readdir $dh) {
next if $entry =~ /^\./; # 跳过隐藏文件
my $path = "$dir/$entry";
if (-d $path) {
find_files($path) unless $entry =~ /^\.{1,2}$/;
} elsif (-f $path) {
process_file($path);
}
}
closedir $dh;
}
sub process_file {
my ($file) = @_;
# ... 同上文件处理逻辑 ...
}
3. 更高级的版本(支持Shebang)
#!/usr/bin/perl
use strict;
use warnings;
my $copyright = <<'COPYRIGHT';
# Copyright (c) 2024 Your Company Name
# All rights reserved.
COPYRIGHT
sub add_copyright {
my ($file) = @_;
open my $fh, '<', $file or return;
my @lines = <$fh>;
close $fh;
# 查找插入位置(在shebang之后)
my $insert_pos = 0;
if ($lines[0] && $lines[0] =~ /^#!/) {
$insert_pos = 1;
# 如果shebang后有空白行,跳过空白行
while ($lines[$insert_pos] && $lines[$insert_pos] =~ /^\s*$/) {
$insert_pos++;
}
}
# 检查是否已有版权信息
for (my $i = 0; $i < @lines && $i < 10; $i++) {
if ($lines[$i] =~ /copyright/i) {
print "$file: Already has copyright (line $i)\n";
return;
}
}
# 插入版权信息
splice @lines, $insert_pos, 0,
($insert_pos > 0 ? "\n" : ""),
$copyright,
"\n";
# 写回文件
open my $out, '>', $file or return;
print $out @lines;
close $out;
print "$file: Copyright added\n";
}
# 使用示例
if (@ARGV) {
add_copyright($_) for @ARGV;
} else {
# 处理当前目录所有Perl文件
my @files = glob("*.pl *.pm *.t");
add_copyright($_) for @files;
}
4. 带配置文件的版本
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my %config = (
year => 2024,
company => "Your Company",
recursive => 0,
backup => 1,
extensions => ['pl', 'pm', 't'],
exclude => [],
);
GetOptions(
'year=i' => \$config{year},
'company=s' => \$config{company},
'recursive' => \$config{recursive},
'backup!' => \$config{backup},
'ext=s' => \@{$config{extensions}},
'exclude=s' => \@{$config{exclude}},
);
my $copyright = generate_copyright($config{year}, $config{company});
sub generate_copyright {
my ($year, $company) = @_;
return <<"END_COPYRIGHT";
# Copyright (c) $year $company
# All rights reserved.
# This software is protected by copyright law and international treaties.
# Unauthorized reproduction or distribution may result in civil and criminal penalties.
END_COPYRIGHT
}
# ... 其余处理逻辑与上面类似 ...
使用建议:
备份文件:使用
-i.bak 参数创建备份
检查现有版权:避免重复添加
保持Shebang:确保#!/usr/bin/perl在第一行
统一格式:使用一致的版权格式
快速使用方法:
# 简单添加
perl -i.bak -pe 'print "# Copyright 2024 Your Company\n\n" if $. == 1' *.pl
# 使用脚本
perl add_copyright.pl --year=2024 --company="My Corp" --recursive .
选择适合你需求的方案,并根据实际情况调整版权信息内容和格式。