Latest Post

Tăng thứ hạng và truy cập tự nhiên với 10 phương pháp SEO hay nhất Kiếm Tiền Online (mmo): Khái Niệm và Các Hình Thức Phổ Biến

Trong chuỗi bài về lập trình bằng Perl, chúng tôi sẽ giới thiệu hướng dẫn sử dụng Perl 5 để thực hiện các thao tác cơ bản trên các tệp văn bản, bao gồm mở, đọc, sửa và xóa tệp.

1. Thao tác với file

Sử dụng function open trong Perl để mở file

– Syntax: open(filehandlemodefilename)
– Mode:

mode operand
read <
write >
append >>

1.1. Mở một file text

Đoạn code để mở file text có tên là sample.txt

use strict;
use warnings;
 
my $filename = 'sample.txt';
my $fh;
if (open($fh, '<', $filename)) {
  print "File ${filename} opened successfully!n";
} else {
  warn "Could not open file '${filename}', Error: $!n";
}
close($fh);

1.2. Mở file text và đọc nội dung một file text

Đoạn code mở file sample.txt và đọc nội dung theo từng line và in ra màn hình

my $filename = 'sample.txt';
my $fh;
if (open($fh, '<', $filename)) {
    while (my $row = <$fh>) {
        chomp $row;
        print "$rown";
    }
} else {
    warn "Could not open file '${filename}', Error: $!n";
}
close($fh);

1.3. Tạo và ghi nội dung vào một file text

Ở trường hợp này

  • Nếu file chưa tồn tại => Tạo file text có nội dung là ‘We are vinasupport.com’
  • Nếu file đã tồn tại => Viêt đè nội dung ‘We are vinasupport.com’ lên file cũ.
my $filename = 'sample.txt';
my $fh;
if (open($fh, '>', $filename)) {
    print $fh 'we are vinasupport.com';
} else {
    warn "Could not create file '${filename}', Error: $!n";
}
close($fh);

1.4. Tạo hoặc chèn nội dung vào một file text đã tồn tại

Ở trường hợp này

  • Nếu file chưa tồn tại => Tạo file text có nội dung là ‘We are vinasupport.com’
  • Nếu file đã tồn tại => Thêm đoạn text ‘We are vinasupport.com’ ở bên dưới nội dung file cũ.
my $filename = 'sample.txt';
my $fh;
if (open($fh, '>>', $filename)) {
    print $fh "we are vinasupport.comn";
} else {
    warn "Could not create file '${filename}', Error: $!n";
}
close($fh);

1.5. Sao chép hoặc đổi tên file

– Sao chép (Copy) file

use File::Copy qw(copy);

my $file_from = 'sample.txt';
my $file_to = 'new_sample.txt';

copy $file_from, $file_to;

– Đổi tên (Rename) file

use File::Copy qw(move);

my $old_file = 'sample.txt';
my $new_file = 'new_sample.txt';

move $old_file, $new_file;

Hoặc đơn giản sử dụng hàm rename built-in sẵn của Perl

rename $old_file, $new_file;

1.6. Xóa file

Sử dụng hàm unlink built-in sẵn của Perl

my $file = 'sample.txt';

unlink $file or die "Could not remove file '${file}'. Error: $!";

2. Thao tác với thư mục

2.1. Tạo thư mục

Sử dụng function mkdir (directory, mask)

my $newDir = 'myFolder';

mkdir($newDir, 0755) or die "Could not create the directory '${newDir}'. Error: $!";

2.2. Tạo thư mục với thư mục con trong nó

Ở trường hợp 2.1 ta chỉ có thể tạo được 1 thư mục, muốn tạo thư mục với thư mục con ở bên trong, tham khảo đoạn code Perl sau

use File::Path 'mkpath';

my $newPath = 'vinasupport.com/Linux/Ubuntu';
my $verbose = 1;
my $mode = 0755;

mkpath($newPath, $verbose, $mode) or die "Could not create the directory '${newPath}'. Error: $!";

Kết quả ta được cấu trúc đường dẫn như sau:

2.3. Xóa thư mục, thư mục con và file trong nó

use File::Path 'rmtree';

my $path = 'vinasupport.com';
my $verbose = 1;
my $safe = 1;

rmtree ($path, $verbose, $safe ) or die "Could not delete the directory '${path}'. Error: $!";

2.4. Liệt kê file trong thư mục

Đoạn code sau sẽ hiển thị danh sách file trong 1 thư mục sử dụng function opendir của Perl

my $dir = '/tmp';

opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {

    # Use a regular expression to ignore files beginning with a period
    next if ($file =~ m/^./);
    print "$filen";
}

closedir(DIR);

Cám ơn các bạn đã theo dõi bài viết

Nguồn vinasupport.com

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *