Skip to content

Commit

Permalink
Perl OOPS, DB, files changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimavanth committed Apr 6, 2020
1 parent 8760e31 commit f442375
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 34 deletions.
48 changes: 24 additions & 24 deletions perl/control-statements/conditional-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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)
{
#code
}
```
### Example
## Example

```perl
$x = 30;
Expand All @@ -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)
Expand All @@ -36,7 +36,7 @@ if(conditional-expression)
#code
}
```
### Example
## Example

```perl
$x = 30;
Expand All @@ -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)
{
Expand All @@ -68,7 +68,7 @@ else {
}
```

### Example
## Example
```perl
$age = 79;

Expand All @@ -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
Expand All @@ -105,7 +105,7 @@ if(conditional-expression-1) {
}
```

### Example
## Example
```perl
$age = 50;
$resident = 'Y';
Expand All @@ -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);
Expand All @@ -133,7 +133,7 @@ unless(condition-expression){
#code
}
```
### Example
## Example
```perl
$age = 35;

Expand All @@ -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){
Expand All @@ -155,7 +155,7 @@ unless(condition-expression){
}
```

### Example
## Example

```perl
$x = 30;
Expand All @@ -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}
Expand Down
10 changes: 5 additions & 5 deletions perl/control-statements/loops.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand Down
30 changes: 25 additions & 5 deletions perl/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
}
]
},
{
"title" : "Arrays",
"path" : "arrays",
"links" : [
{
"title" : "Arrays",
"path" : "arrays"
}
]
},
{
"title" : "Strings",
"path" : "strings",
Expand Down Expand Up @@ -88,7 +98,7 @@
"path" : "sub-routine",
"links" : [
{
"title" : "Sub-routine",
"title" : "Sub-routines",
"path" : "sub-routine"
}
]
Expand All @@ -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"
}
]
}
Expand Down
77 changes: 77 additions & 0 deletions perl/oops-in-perl/oops.md
Original file line number Diff line number Diff line change
@@ -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.
Loading

0 comments on commit f442375

Please sign in to comment.