Perl 替换字符串应用

  • Post author:
  • Post category:其他


Cache all in Perl

use strict;
use warnings;

print "input filename :  \n";

my $filename = <STDIN>;
chomp $filename;# drop \n 

my $data = read_file($filename);

$data =~ s/ld.global/ld.global.ca/g;
write_file($filename, $data);
exit;

sub read_file {
    my ($filename) = @_;

    open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!";
    local $/ = undef;
    my $all = <$in>;
    close $in;

    return $all;
}

sub write_file {
    my ($filename, $content) = @_;

    open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";;
    print $out $content;
    close $out;

    return;
}



版权声明:本文为dark5669原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。