امثلة على التعامل مع الملفات في لغة Perl

  • 2020-10-14

توصيف

كود انشاء ملف :

my $filename = 'file1.txt';  
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";  
print $fh "Hello!! We have created this file as an example\n";  
close $fh;  
print "done\n";  

 

كود قراءة سطر واحد من ملف :

use strict;  
use warnings;  
my $filename = 'file1.txt';  
open(my $fh, '<:encoding(UTF-8)', $filename)  
  or die "Could not open file '$filename' $!";  
my $row = <$fh>;  
print "$row\n";  
print "done\n";  

 

كود قراءة عدة اسطر من ملف:

use strict;  
use warnings;  
my $filename = 'file1.txt';  
open(my $fh, '<:encoding(UTF-8)', $filename)  
  or die "Could not open file '$filename' $!";  
while (my $row = <$fh>) {  
  chomp $row;  
  print "$row\n";  
}  
print "done\n";  

 

كود الكتابة في ملف :

open (FILE, ">> file1.txt") || die "problem opening $file1.txt\n";  
print FILE "This line is added in the file1.txt\n";  
# FILE array of lines is written here  
print FILE @lines1;  
# Another FILE array of lines is written here  
print FILE "A complete new file is created";  
# write a second array of lines to the file  
print FILE @lines2;  

 

هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...