How to Read 5 Symbols From Line 3 Perl

Perl Read File

Introduction to Perl Read File

Perl read file is used to read the content of a file, we have to assign file handler on the file to perform diverse file operations on the file. File reading operations is very important and useful to read the content of the file. There are basically three types of methods bachelor to read the file i.eastward. using file handler operator, using getc function, and using read function. File handler using the operator is the nearly important and useful method bachelor in Perl to read any file.

How to Read File in Perl Using Various Methods?

Below is the various method which was available to read a file are equally follows. File reading operations is very important and useful in Perl to read the content of file.

1. Below is the mode to read and opened the file to admission for diverse operations on file.

  • < – This operator is used to read only access on the file.
  • > – This operator is used to creates, writes and truncates the file.
  • >> – This operator is used to writes, append, and creates the file.
  • +< – This operator is used to read and writes the file at the aforementioned fourth dimension.
  • +> – This operator is used to read, writes, creates, and truncates the file.
  • +>> – This operator is used to read, writes, creates, and appends the file.

2. Below is the different types of ways or methods available to read the file are as follows.

  • Using file handler operator
  • Using getc part
  • Using read office

3. In Perl read file is used to read the content of a file, in Perl we have assigning file handler to perform the various file operations on the file.

4. File handler using the operator is the most important and useful method available to read whatsoever file in Perl.

Methods of Perl Read File

Below we accept discussed the method of read file are equally follows.

one. Using File Handler Operator

  • Read file using operator is most important and useful method to read file in Perl.
  • This is the primary and of import method which is used to read file. Using this method we accept read file using the operator.
  • Nosotros have use <> operator to read file in Perl. This operator is very of import at the fourth dimension of read file.
  • While using <> operator in Perl it will render the list or multiple lines from the specified file handler.
  • File handler operator i.east. <> used in list context of the file. This operator is used in a list context of the file.
  • Using <> operator in Perl nosotros have reading single or multiple lines from the file and store it in scalar format.
  • The below example shows that read file lines from the abc.txt file and store the content of the file in scalar format.

Example:

Nosotros have using the below sample text file to describe example read methods are as follows.

Perl Read File Example 1

Code:

# Open file name as abc.txt to read first line content using file handler operator.
open(fh, "abc.txt") or die "Not opening";
# Reading first line of the file using <> operator in Perl.
$firstline = <fh>;
print "$firstline\n";

Output:

Perl Read File Example 2

In the higher up instance, we accept using the abc.txt file to depict the example of read file handler operator. We have read the commencement line from the file using the <> operator in Perl.

2. Using getc Part

  • Read file using getc part is most important and useful to read file.
  • This is the chief and important method which is used to read file. Using this method we take read file using the function.
  • Getc office in the read file will return a single character from the file. It volition return the first graphic symbol from the get-go line.
  • This role is virtually useful when nosotros take read a single graphic symbol from the file.
  • If the mistake occurred in the file which we accept used with getc function then it will return an undef as output to the user.
  • Getc function returns a unmarried graphic symbol from the specified file handler or STDIN if we have not specified a part.
  • The below example shows that the read file first grapheme from the abc.txt file is equally follows.

Example:

 Code:

# Open up file name as abc.txt to read first character using getc function in Perl.
open(fh, "abc.txt") or dice "Not opening";
# Reading first graphic symbol of the file using getc office in Perl.
$firstchar_getc = getc(fh);
impress "$firstchar_getc\due north";

Output:

getc Function Example 3

In the in a higher place case, we have to utilise the abc.txt file to describe the example of read file using getc function in Perl. We accept read the commencement character from the file using getc function in Perl.

three. Using Read Function

  • Read file using read function is almost of import and useful to read file.
  • This is the main and important method which is used to read file. Using this method, nosotros have read file using the read function.
  • We have read multiple lines from file using a read function. We take read multiple line at one time using read function.
  • Using read function we will read the content of file by the file handler till if information technology does non accomplish the end of the file.

Example:

Code:

# Open file name every bit abc.txt to read first graphic symbol using getc function in Perl.
open(FH, "abc.txt")or dice "Non opening";
print "Reading file \north";
# Reading the file till information technology not reaches to the stop of file.
while(<FH>)
{
# Using beneath operator it will print single line at ane fourth dimension.
print $_;
}
close;

Output:

Read Function Example 4

In the above example, we have to employ the abc.txt file to depict the example of read file using the read function.

Conclusion

File reading operations is very of import and useful to read the content of file. Perl read file is used to read the content of a file, in Perl we have to assign file handler on the file to perform various file operations on the file.

Recommended Articles

This is a guide to Perl Read File. Here nosotros discuss a cursory overview on Perl Read File and its dissimilar methods along with examples and code Implementation. You tin also go through our other suggested articles to learn more –

  1. Perl Variables
  2. Perl Versions
  3. Perl vs Red

How to Read 5 Symbols From Line 3 Perl

Source: https://www.educba.com/perl-read-file/

0 Response to "How to Read 5 Symbols From Line 3 Perl"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel