Perl: File Handling (Open, Split, @ARGV) - by Eun Bae Kim (07/26/2018)
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  
use strict;

# @ARGV
# Run this file as follows;
# perl ex004.pl example004.txt
# Contents of example004.txt
# EBKim	50	30	70
# GDJin	55	40	60
# IHYoo	45	55	70


# Get an Argument
my $sFileToOpen = $ARGV[0];     # Alternatively, my $sFileToOpen = shift;
print "--------------------------------------------\n";
print "File to open: ".$sFileToOpen."\n";


# Open a file for reading
open(hIn, $sFileToOpen) or die;
print "--------------------------------------------\n";
while (my $sLine = <hIn>) {
	print $sLine;        # This line includes "\n" and "\r".
}
print "\n";
close(hIn);

# Line symbols
open(hIn, $sFileToOpen) or die;
print "--------------------------------------------\n";
while (my $sLine = <hIn>) {
	$sLine=~s/\n//g;     # Remove "\n" from the current line
	$sLine=~s/\n//g;     # Remove "\r" from the current line

	print $sLine."===";
	print $sLine."\n";
}
close(hIn);




# Splitting a line
open(hIn, $sFileToOpen) or die;
my @aTemp = ();
my $iCnt  = 0;
print "--------------------------------------------\n";
print "Opening: ".$sFileToOpen."\n";
print "-----------------------------------\n";
while (my $sLine = <hIn>) {
	$iCnt++;
	print "Line: ".$iCnt."\n";

	$sLine=~s/\n//g;     # Remove "\n" from the current line
	$sLine=~s/\n//g;     # Remove "\r" from the current line

	print "Original: ".$sLine."\n";

	my @aLine = split("\t", $sLine);
	@aTemp = @aLine;     # Save information in a temporary array
	print "Split:    ";
	foreach my $sCurElement (@aLine) {
		print $sCurElement."=";
	}
	print "\n";

	my $sNewLine = join("-", @aLine);
	print "Join:     ".$sNewLine."\n";
	print "-----------------------------------\n";
}
close(hIn);






# Writing a file
my $sFileToSave = "example004_save.txt";
print "--------------------------------------------\n";
print "Writing: ".$sFileToSave."\n";
print "-----------------------------------\n";
open(hOut, ">".$sFileToSave) or die;
foreach my $sCurElement (@aTemp) {
	print "Screen: ".$sCurElement."\n";
	print hOut $sCurElement."\n";
}
close(hOut);



# For more information, refer to the following link
# https://www.tutorialspoint.com/perl