File handle in PROGRAMMable extensible interpreted .pptx
urvashipundir04
11 views
32 slides
Sep 03, 2024
Slide 1 of 32
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
About This Presentation
File handle in PROGRAMMable extensible interpreted
Size: 274.37 KB
Language: en
Added: Sep 03, 2024
Slides: 32 pages
Slide Content
Virtual function // C++ program to illustrate // concept of Virtual Functions #include < iostream > using namespace std ; class base { public: virtual void print() { cout << "print base class\n"; } void show() { cout << "show base class\n"; } };
class derived : public base { public: void print() { cout << "print derived class\n"; } void show() { cout << "show derived class\n"; } };
int main() { base* bptr ; derived d; bptr = &d; // Virtual function, binded at runtime bptr ->print(); // Non-virtual function, binded at compile time bptr ->show(); return 0; } OUTPUT: PRINT derived class Show base clase
File handle in PERL
First program in PERL Perl programming language is exclusively designed for text processing purposes. Its abbreviation denotes Practical Extraction and Report Language . It is compatible on various platforms, such as Windows, Mac OS, and almost all versions of UNIX.To write perl program Step 1: Transfer the file to Perl Interpreter : Always in Perl, the first line starts with a pair of characters #!. It insists Perl interpreter how the file should be executed. Here, the file should be transferred to Perl interpreter that resides in / usr /bin/ perl folder . #!/ usr /bin/ perl
Step 2: Pragma in Perl : A pragma is a specific module in Perl package which has the control over some functions of the compile time or Run time behavior of Perl, which is strict or warnings. So the next two lines go like this , Use strict; Use warnings; Step 3: Use of print() function : Print(“HELLO WROLD /n” );
#!/ usr /bin/ perl # Modules used use strict; use warnings; # Print function print("Hello World\n"); OUTPUT: Hello World
Important Points: The file must be stored with an extension of . pl Directory of the Perl Package must be same as to where the Program file is saved .
How to Run a Perl Program? First, open a text editor like Notepad or Notepad++. Write the code in the text editor and save the file with . pl extension Make sure you have downloaded and installed the latest version of Perl from https://www.perl.org/get.html Open commandline and Run the command perl -v to check if your Perl’s latest version has been installed properly or not.
In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files.
File modes: When opening a file in Perl, you need to specify a file mode, which determines how the file can be accessed. There are three main file modes: Read mode (<): Opens the file for reading. The file must already exist. Write mode (>): Opens the file for writing. If the file does not exist, it will be created. If the file already exists, its contents will be truncated. Append mode (>>): Opens the file for writing, but appends new data to the end of the file instead of overwriting its contents. If the file does not exist, it will be created.
File handling functions: open(): Opens a file and returns a file handle. close(): Closes a file handle. print(): Writes data to a file. read(): Reads data from a file. seek(): Moves the file pointer to a specific location in the file. tell(): Returns the current position of the file pointer. stat(): Returns information about a file, such as its size, owner, and permissions.
In Perl a FileHandle associates a name to an external file, that can be used until the end of the program or until the FileHandle is closed. The three basic FileHandles in Perl are STDIN, STDOUT, and STDERR, which represent Standard Input, Standard Output, and Standard Error devices respectively. File Handling is usually done through the open function . Syntax: open( FileHandle , Mode, FileName ); Parameters: FileHandle - The reference to the file, that can be used within the program or until its closure. Mode- Mode in which a file is to be opened. FileName - The name of the file to be opened.
Also, Mode and FileName can be clubbed to form a single expression for opening. Syntax: open( FileHandle , Expression); Parameters: FileHandle - The reference to the file, that can be used within the program or until its closure. Expression- Mode and FileName clubbed together.
Syntax: close( FileHandle ); Parameters: FileHandle - The FileHandle to be closed.
Different Modes in File Handling Mode Explanation “<“ Read Only Mode “>” Creates file (if necessary), Clears the contents of the File and Writes to it “>>” Creates file (if necessary), Appends to the File “+<“ Reads and Writes but does NOT Create “+>” Creates file (if necessary), Clears, Reads and Writes “+>>” Creates file (if necessary), Reads and Appends
Perl data type Perl is loosely typed language and there is no need to specify a type for your data while using in your program. The Perl interpreter will choose the type based on the context of the data itself. Perl has three basic data types : Scalars arrays of scalars hashes of scalars, also known as associative arrays. Here is little detail about these data types.
Scalars: Scalars are single values. They can be strings, numbers, or references. Scalars are represented using a dollar sign ($) . Examples include: Strings: $name = "John"; Numbers: $age = 30; References: $ array_ref = [1, 2, 3]; Arrays: Arrays are ordered lists of scalars. They are represented using an at sign (@) . Examples include: @numbers = (1, 2, 3); @names = ("Alice", "Bob", "Charlie"); Hashes: Hashes are unordered collections of key-value pairs. They are represented using a percent sign (%) . Examples include: %person = ("name" => "Alice", "age" => 25); %grades = ("Alice" => 90, "Bob" => 85, "Charlie" => 95); References: References are scalar values that refer to other data structures (scalars, arrays, hashes, or even other references). They are represented using a backslash () sigil. Examples include: Reference to a scalar: $ scalar_ref = \$name; Reference to an array: $ array_ref = \@numbers; Reference to a hash: $ hash_ref = \%person;
Examples: Consider a file Hello.txt containing the string “Welcome to PERL PROGRAMMING” initially . #!/ usr /bin/ perl # Opening a File in Read-only mode open(r, "<", "Hello.txt"); # Printing content of the File print(<r>); # Closing the File close(r);
Perl | Loops (for, foreach , while, do…while, until, Nested loops) Looping in programming languages is a feature which facilitates the execution of a set of instructions or functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Perl provides the different types of loop to handle the condition based situation in the program. The loops in Perl are : “for” loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.
SYNTAX For( init statement; condition ; increment/decrement ) { Code to be executed }
Working
init statement: This is the first statement which is executed. In this step, we initialize a variable which controls the loop. condition: In this step, the given condition is evaluated and the for loop runs if it is True. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements. Statement execution: Once the condition is evaluated to true, the statements in the loop body are executed. increment/decrement: The loop control variable is changed here (incremented or decremented) for updating the variable for next iteration. Loop termination: When the condition becomes false, the loop terminates marking the end of its life cycle.
#!/ usr /bin/ perl # Perl program to illustrate # the for loop # for loop for ($count = 1 ; $count <= 3 ; $count+=1) { print (“HELLO WORLD"); }
while Loop A while loop generally takes an expression in parenthesis. If the expression is True then the code within the body of while loop is executed. A while loop is used when we don’t know the number of times we want the loop to be executed however we know the termination condition of the loop. It is also known as a entry controlled loop as the condition is checked before executing the loop. The while loop can be thought of as a repeating if statement. Syntax :
Syntax : while (condition) { # Code to be executed }
EXAPLE # Perl program to illustrate # the while loop # while loop $count = 3; while ($count >= 0) { $count = $count - 1; print “HELLO WORLD\n "; }
Infinite While Loop Infinite While Loop: While loop can execute infinite times which means there is no terminating condition for this loop. In other words, we can say there are some conditions which always remain true, which causes while loop to execute infinite times or we can say it never terminates.
# Perl program to illustrate # the infinite while loop # infinite while loop # containing condition 1 # which is always true while(1) { print "Infinite While Loop\n"; }