From 5da552b412428d6cef8644db1a37d0c38b639891 Mon Sep 17 00:00:00 2001 From: Daniel Mita Date: Tue, 18 Jun 2024 02:02:47 +0100 Subject: [PATCH] Add content to new concepts --- concepts/booleans-and-conditionals/about.md | 19 +++++++ .../booleans-and-conditionals/introduction.md | 19 +++++++ concepts/object-orientation/about.md | 54 +++++++++++++++++++ concepts/object-orientation/introduction.md | 54 +++++++++++++++++++ 4 files changed, 146 insertions(+) diff --git a/concepts/booleans-and-conditionals/about.md b/concepts/booleans-and-conditionals/about.md index e69de29b..d4979fa3 100644 --- a/concepts/booleans-and-conditionals/about.md +++ b/concepts/booleans-and-conditionals/about.md @@ -0,0 +1,19 @@ +# About + +## Booleans + +Boolean values exist in Perl, however they are seldom used explicitly. +They typically come as the result of a comparison operation, e.g, `2 > 1` or `'foo' eq 'bar'`, or negation, e.g., `!0`. + +`1` and `0` are commonly used to represent true and false in boolean contexts. +With `use v5.40` or newer, the functions `true` and `false` are included to provide explicit boolean values. + +The following are considered false in Perl: + +```perl +0; # The number 0. +'0'; # A single 0 in a string. +''; # An empty string. +(); # An empty list. +undef; +``` diff --git a/concepts/booleans-and-conditionals/introduction.md b/concepts/booleans-and-conditionals/introduction.md index e69de29b..7101830c 100644 --- a/concepts/booleans-and-conditionals/introduction.md +++ b/concepts/booleans-and-conditionals/introduction.md @@ -0,0 +1,19 @@ +# Introduction + +## Booleans + +Boolean values exist in Perl, however they are seldom used explicitly. +They typically come as the result of a comparison operation, e.g, `2 > 1` or `'foo' eq 'bar'`, or negation, e.g., `!0`. + +`1` and `0` are commonly used to represent true and false in boolean contexts. +With `use v5.40` or newer, the functions `true` and `false` are included to provide explicit boolean values. + +The following are considered false in Perl: + +```perl +0; # The number 0. +'0'; # A single 0 in a string. +''; # An empty string. +(); # An empty list. +undef; +``` diff --git a/concepts/object-orientation/about.md b/concepts/object-orientation/about.md index e69de29b..33ae5f65 100644 --- a/concepts/object-orientation/about.md +++ b/concepts/object-orientation/about.md @@ -0,0 +1,54 @@ +# About + +## Classes + +Class-based object orientation is a paradigm often used in Perl. +Traditionally, classes in Perl are created by using `bless` to associate a reference with a package. + +```perl +package Foo; + +sub new ($class, %args) { + return bless {%args{bar}}, $class; +} + +sub bar ($self) { + return $self->{bar}; +} +``` + +Perl `v5.38` introduced the `class` keyword which brings additional OOP features to core Perl. + +```perl +use feature qw; + +class Foo; + +field $bar :param; + +method bar () { + return $bar; +} +``` + +Additionally, Perl has numerous modules available on CPAN implementing OOP features. +Among the most well known are `Moo` and `Moose`. + +```perl +package Foo; + +use Moo; + +has bar => (is => 'ro'); +``` + +The arrow operator (`->`) is used to call methods on class names and objects. +All of the above can be used as follows: + +```perl +# Object creation +my $object = Foo->new(bar => 1); + +# Method call +say $object->bar; +``` diff --git a/concepts/object-orientation/introduction.md b/concepts/object-orientation/introduction.md index e69de29b..e2cfdbed 100644 --- a/concepts/object-orientation/introduction.md +++ b/concepts/object-orientation/introduction.md @@ -0,0 +1,54 @@ +# Introduction + +## Classes + +Class-based object orientation is a paradigm often used in Perl. +Traditionally, classes in Perl are created by using `bless` to associate a reference with a package. + +```perl +package Foo; + +sub new ($class, %args) { + return bless {%args{bar}}, $class; +} + +sub bar ($self) { + return $self->{bar}; +} +``` + +Perl `v5.38` introduced the `class` keyword which brings additional OOP features to core Perl. + +```perl +use feature qw; + +class Foo; + +field $bar :param; + +method bar () { + return $bar; +} +``` + +Additionally, Perl has numerous modules available on CPAN implementing OOP features. +Among the most well known are `Moo` and `Moose`. + +```perl +package Foo; + +use Moo; + +has bar => (is => 'ro'); +``` + +The arrow operator (`->`) is used to call methods on class names and objects. +All of the above can be used as follows: + +```perl +# Object creation +my $object = Foo->new(bar => 1); + +# Method call +say $object->bar; +```