diff --git a/perl/control-statements/conditional-statements.md b/perl/control-statements/conditional-statements.md index f2737f7..6821718 100644 --- a/perl/control-statements/conditional-statements.md +++ b/perl/control-statements/conditional-statements.md @@ -2,9 +2,9 @@ When ever you want to perform a set of operations based on a condition(s) If / I You can also use if-else , nested IFs and IF-ELSE-IF ladder when multiple conditions are to be performed on a single variable. -# If +# 1. If -### Syntax +## Syntax ```perl if(conditional-expression) @@ -12,7 +12,7 @@ if(conditional-expression) #code } ``` -### Example +## Example ```perl $x = 30; @@ -22,11 +22,11 @@ if(conditional-expression) print "x and y are equal"; } ``` -#### Check result [here](https://onecompiler.com/perl/3vnufqbpw) +### Check result [here](https://onecompiler.com/perl/3vnufqbpw) -## If-else +# 2. If-else -### Syntax +## Syntax ```perl if(conditional-expression) @@ -36,7 +36,7 @@ if(conditional-expression) #code } ``` -### Example +## Example ```perl $x = 30; @@ -48,11 +48,11 @@ if ( $x == $y) { print "x and y are not equal"; } ``` -#### Check result [here](https://onecompiler.com/perl/3vnufv3jb) +### Check result [here](https://onecompiler.com/perl/3vnufv3jb) -## If-else-if ladder +# 3. If-else-if ladder -### Syntax +## Syntax ```perl if(conditional-expression-1) { @@ -68,7 +68,7 @@ else { } ``` -### Example +## Example ```perl $age = 79; @@ -86,13 +86,13 @@ else { print "Invalid $age"; } ``` -#### Check result [here](https://onecompiler.com/perl/3vnufzdz5) +### Check result [here](https://onecompiler.com/perl/3vnufzdz5) -## Nested-If +# 4. Nested-If Nested-Ifs represents if block within another if block. -### Syntax +## Syntax ```perl if(conditional-expression-1) { #code @@ -105,7 +105,7 @@ if(conditional-expression-1) { } ``` -### Example +## Example ```perl $age = 50; $resident = 'Y'; @@ -116,13 +116,13 @@ if(conditional-expression-1) { } } ``` -#### Check result [here](https://onecompiler.com/perl/3vnugaqs2) +### Check result [here](https://onecompiler.com/perl/3vnugaqs2) -## Unless +# 5. Unless Unless is similar to If and is equivalent to `if-not`. Perl executes the code block if the condition evaluates to false, otherwise it skips the code block. -### Syntax +## Syntax ```perl statement unless(condition-expression); @@ -133,7 +133,7 @@ unless(condition-expression){ #code } ``` -### Example +## Example ```perl $age = 35; @@ -143,9 +143,9 @@ unless($age < 18){ ``` ### Check Result [here](https://onecompiler.com/perl/3vnuj6xme) -## Unless-else +# 6. Unless-else -### syntax +## syntax ```perl unless(condition-expression){ @@ -155,7 +155,7 @@ unless(condition-expression){ } ``` -### Example +## Example ```perl $x = 30; @@ -169,11 +169,11 @@ unless ( $x == $y) { ``` ### Check result [here](https://onecompiler.com/perl/3vnujet2w) -## Given +# 7. Given Given is similar to Switch in other programming languages. Given is an alternative to IF-ELSE-IF ladder and to select one among many blocks of code. -### Syntax +## Syntax ```perl given(conditional-expression){ when(value1){#code} diff --git a/perl/control-statements/loops.md b/perl/control-statements/loops.md index ff2b3d7..8fde856 100644 --- a/perl/control-statements/loops.md +++ b/perl/control-statements/loops.md @@ -1,4 +1,4 @@ -# For +# 1. For For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance. In perl, `for` and `foreach` loops are interchangeable, and hence you can use foreach loop in where you use the for loop. @@ -51,7 +51,7 @@ for(keys %nationalGame){ ``` ### Check result [here](https://onecompiler.com/perl/3vnwnghgy) -# While +# 2. While While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations is not known in advance. @@ -74,7 +74,7 @@ while ( $i <= 5) { ``` ### Check result [here](https://onecompiler.com/perl/3vnwnmp75) -# Do-while +# 3. Do-while Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once. @@ -99,7 +99,7 @@ do { ### Check result [here](https://onecompiler.com/perl/3vnwntcpj) -# Until +# 4. Until Until is just opposite of while loop and executes a block of code as long as the condition is false. @@ -122,7 +122,7 @@ until ( $i > 5) { ``` ### Check result [here](https://onecompiler.com/perl/3vnwpabyg) -# Do-Until +# 5. Do-Until Do-Until is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once and it executes a block of code as long as the condition is false. diff --git a/perl/index.json b/perl/index.json index c692527..10fb225 100644 --- a/perl/index.json +++ b/perl/index.json @@ -29,6 +29,16 @@ } ] }, + { + "title" : "Arrays", + "path" : "arrays", + "links" : [ + { + "title" : "Arrays", + "path" : "arrays" + } + ] + }, { "title" : "Strings", "path" : "strings", @@ -88,7 +98,7 @@ "path" : "sub-routine", "links" : [ { - "title" : "Sub-routine", + "title" : "Sub-routines", "path" : "sub-routine" } ] @@ -104,12 +114,22 @@ ] }, { - "title" : "Regular expressions", - "path" : "regular-expressions", + "title" : "Working with Databases", + "path" : "working-with-databases", + "links" : [ + { + "title" : "Databases", + "path" : "databases" + } + ] + }, + { + "title" : "Working with Files", + "path" : "working-with-files", "links" : [ { - "title" : "Regular expressions", - "path" : "reg-ex" + "title" : "Files I/O", + "path" : "files" } ] } diff --git a/perl/oops-in-perl/oops.md b/perl/oops-in-perl/oops.md new file mode 100644 index 0000000..8f19e7d --- /dev/null +++ b/perl/oops-in-perl/oops.md @@ -0,0 +1,77 @@ +Perl supports Object oriented concepts. Perl object oriented concepts are based on references, anonymous arrays and hashes. + +# 1. Classes and objects + +Object is a basic unit in OOP, and is a single entity which combines data and actions. They have state and behaviour. Consider mobile as an object and let's see an example to understand state and behaviour. + +State of the objects can be represented by variables and behaviour of the objects can be represented by methods. Object is a real-world or run-time entity. + +**Object** : Mobile +**State** : Model, Brand, Color, Size +**Behaviour** : Make a call, send a message, Click a picture + +Class is a package which contains methods used to create and manipulate objects. In order to create a class, you need to build a package. + +### How to create a Class + +`package` keyword is required to create a class. + +```perl +package className; +``` +### How to create a Object + +```perl +$object = new className( Attributes); +``` +### How to define methods in a class + +```perl +sub methodName{ + #code +} +``` +## Example + +```perl +package Mobile; # creating Class + +sub new { + my $class = shift; + my $self = { + _brandName => shift, + _modelName => shift, + _price => shift, + }; + print "Brand name of the Mobile is $self->{_brandName}\n"; + print "Model name of the Mobile is $self->{_modelName}\n"; + print "Price is $self->{_price}\n"; + bless $self, $class; + return $self; +} + +$object = new Mobile( "iPhone", "iPhone 11 Pro", "999 dollars"); # creating Object +``` + +## Check Result [here](https://onecompiler.com/perl/3vp87uvsq) + +# 2. Inheritance + +Inheritance is a process of acquiring parent properties and behaviour by a child automatically. Hence you can either reuse or extend parent properties or behaviour in the child class. The main advantage of inheritance is code reusability. Perl has a special variable called `@ISA`. Consider we have a class named `Dogs` which inherits from `Animals` base class. + +```perl +package Dogs; +use Animals; +use strict; +our @ISA = qw(Animals); # inherits from Animals +``` + +# 3. Encapsulation + +Encapsulation is a mechanism to protect private hidden data from other users. It wraps the data and methods as a single bundle. You can hide the complexity using objects in object oriented programming. Client of the objects will not know the internal logic and still can use the object using it's methods. + +# 4. Polymorphism + +Polymorphism gives the meaning many forms, usually it occurs when multiple classes are present and have been inherited. + +Consider an example of installing an application in your mobile, where your base class is `Mobile` and method is `installation()` and its derived classes could be installApp1, installApp2, installApp3 etc which will have its own implementation but installation method can be common which can be inherited from its base class. diff --git a/perl/working-with-databases/databases.md b/perl/working-with-databases/databases.md new file mode 100644 index 0000000..7b8d8da --- /dev/null +++ b/perl/working-with-databases/databases.md @@ -0,0 +1,87 @@ +Perl DBI(Database Independent Interface for Perl) module helps to interact with many of databases including Oracle, SQL Server, MySQL, Sybase, etc. + +In this chapter, let's learn how to interact with MySQL database in Perl. + +## How to install DBD::mysql Module + +* Install MySQL driver to connect to MySQL databases(assuming you have MySQL server installed on your machine.) + +There are multiple ways to install MySQL drivers, one of the ways is by using `CPAN`. + +```sh +perl -MCPAN -e shell +``` +* To install `DBD::mysql` use the below command, + +```sh +install DBD:mysql +``` +* Installation progress can be seen in command prompt. + +## How to connect to MySQL database server + +Consider we are creating a sample database called `test` + +```sh +my $dbh = DBI->connect("DBI:mysql:test",'root',''); +``` + +DBI->connect has three parameters: +1. Data source name +2. Username +3. Password + +DBI->connect returns false when the connection is not established. + +## Insert data in to the database + +Once the connection is established, you can insert data into the tables. Consider you are inserting data into DATA_TABLE +```perl +my $sth = $dbh->prepare("INSERT INTO DATA_TABLE + (NAME, ID, OCCUPATION ) + values + ('foo', 123, 'doctor')"); +$sth->execute() or die $DBI::errstr; +print "Inserted rows:" + $sth->rows; +$sth->finish(); +$dbh->commit or die $DBI::errstr; +``` + +## Read data from tables + +```perl +my $sth = $dbh->prepare("SELECT NAME, ID + FROM DATA_TABLE + WHERE OCCUPATION = 'doctor'"); +$sth->execute() or die $DBI::errstr; +print "Number of records whose occupation is doctor :" + $sth->rows; +while (my @row = $sth->fetchrow_array()) { + my ($name, $id ) = @row; + print "Name = $name, ID = $id\n"; +} +$sth->finish(); +``` + +## Updating a record + +```perl +my $sth = $dbh->prepare("UPDATE DATA_TABLE + SET OCCUPATION = 'student' + WHERE ID = 123"); +$sth->execute() or die $DBI::errstr; +print "Updated rows:" + $sth->rows; +$sth->finish(); +$dbh->commit or die $DBI::errstr; +``` + +## Deleting a record + +```perl +my $sth = $dbh->prepare("DELETE FROM DATA_TABLE + WHERE ID = 123"); +$sth->execute( $id ) or die $DBI::errstr; +print "Deleted rows:" + $sth->rows; +$sth->finish(); +$dbh->commit or die $DBI::errstr; +``` + diff --git a/perl/working-with-files/files.md b/perl/working-with-files/files.md new file mode 100644 index 0000000..c3fece9 --- /dev/null +++ b/perl/working-with-files/files.md @@ -0,0 +1,67 @@ +Filehandle is used to open or close a file in Perl. + +## How to Open a file? + +```perl +open(filehandle,mode,filename) +``` +* filehandle is a variable which associates with the file +* Below are the modes available in Perl + +|Mode|Symbol| +|----|----| +|read| < | +|write| > | +|append| >>| + +* Filename is the name of the file you want to open along with filepath. + +### Example + +Consider you want to read `sample.txt` file: + +```perl +open(FH, '<', 'c:\sample.txt'); +``` + +## How to close a file? + +You must close the file after finishing read or write operations on the file. `close()` function is used to close a file. + +```perl +close(FH); +``` + +## File test Operators + +Below are some of the frequently used test operators which helps in checking about the file before performing read or write operations: + +|File test Operator| Description| +|----|----| +|-r| checks if the file is readable| +|-w| checks if the file is writable| +|-x| checks if the file is executable| +|-o| checks if the file is owned by effective uid.| +|-T| checks if the file is an ASCII text file.| +|-B| checks if the file is a binary file.| +|-e| checks if the file exists.| +|-z| checks if the file is empty.| +|-s| checks if the file has nonzero size.| +|-f| checks if the file is a plain file.| +|-d| checks if the file is a directory.| +|-l| checks if the file is a symbolic link.| +|-p| checks if the file is a named pipe (FIFO.| +|-S| checks if the file is a socket.| +|-b| checks if the file is a block special file.| +|-c| checks if the file is a character special file.| + +### Example + +```perl +my $filename = 'c:\sample.txt'; +if(-e $filename && -r _){ + print("$filename is present and readable \n"); +}else{ + print("$filename is neither present nor readable\n"); +} +```