If you have programmed using object oriented programming before, then you will be aware of the need to create a destructor to free the memory allocated to the object when you have finished using it. Perl does this automatically for you as soon as the object goes out of scope.In case you want to implement your destructor, which should take care of closing files or doing some extra processing then you need to define a special method called DESTROY. This method will be called on the object just before Perl frees the memory allocated to it. In all other respects, the ... Read More
Perl offers a feature which you would not find in any other programming languages: a default subroutine. Which means, if you define a function called AUTOLOAD(), then any calls to undefined subroutines will call AUTOLOAD() function automatically. The name of the missing subroutine is accessible within this subroutine as $AUTOLOAD.Default autoloading functionality is very useful for error handling. Here is an example to implement AUTOLOAD, you can implement this function in your own way.sub AUTOLOAD { my $self = shift; my $type = ref ($self) || croak "$self is not an object"; my $field = $AUTOLOAD; $field ... Read More
You can add your additional functions in child class or you can add or modify the functionality of an existing methods in its parent class. It can be done as follows −#!/usr/bin/perl package Employee; use Person; use strict; our @ISA = qw(Person); # inherits from Person # Override constructor sub new { my ($class) = @_; # Call the constructor of the parent class, Person. my $self = $class->SUPER::new( $_[1], $_[2], $_[3] ); # Add few more attributes $self->{_id} = undef; $self->{_title} = undef; bless $self, $class; return $self; } # ... Read More
Object-oriented programming has very good and useful concept called inheritance. Inheritance simply means that properties and methods of a parent class will be available to the child classes. So you don't have to write the same code again and again, you can just inherit a parent class.For example, we can have a class Employee, which inherits from Person. This is referred to as an "isa" relationship because an employee is a person. Perl has a special variable, @ISA, to help with this. @ISA governs (method) inheritance.Following are the important points to be considered while using inheritance −Perl searches the class ... Read More
Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and they provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper methods to manipulate object data.Lets define a helper method to get person’s first name −sub getFirstName { return $self->{_firstName}; }Another helper function to set person’s first name −sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; }Now lets have a look into complete example: ... Read More
To create an instance of a class (an object) we need an object constructor in Perl. This constructor in Perl is a method defined within the package. Most programmers choose to name this object constructor method new, but in Perl you can use any name.You can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes.Let's create our constructor for our Person class using a Perl hash reference. When creating an object, you need to supply a constructor, which is a subroutine within a package that returns an object ... Read More
Whenever an Express application server receives an HTTP request, it will provide the developer with an object, commonly referred to as res. For example, Exampleapp.get('/test', (req, res) => { // use req and res here })The res object basically refers to the response that'll be sent out as part of this API call.The res.send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.The res.json function on the other handsets the content-type header to application/JSON so that the client treats the response string ... Read More
A local file is a .json file that contains a set of translations for the text strings used in a theme template file. A separate local file is used for every language.When you require moment.js in your code and pack it with webpack, the bundle size becomes huge because it includes all locale files.You can remove all locale files using the IgnorePlugin. For example, Exampleconst webpack = require('webpack'); module.exports = { plugins: [ // Ignore all locale files of moment.js new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), ], }; // load specific locales in your code. ... Read More
Arrays can be compared in 2 ways −They refer to the same array object in memory.They may refer to different objects but their contents are all equal.For case 1, jasmine provides the toBe method. This checks for reference. For example, Exampledescribe("Array Equality", () => { it("should check for array reference equility", () => { let arr = [1, 2, 3]; let arr2 = arr // Runs successfully expect(arr).toBe(arr2); // Fails as references are not equal expect(arr).toBe([1, 2, 3]); }); });OutputThis ... Read More
You can't break from each method. It copies the native forEach method's behavior and the native forEach doesn't provide to escape the loop (other than throwing an exception).You can use other functions like −_.find: it breaks out of the loop when the element is found. For example,_.find([1, 2, 3, 4], (element) => { // Check your condition here if (element === 2) { return true; } // Do what you want with the elements here // ... });Throw an exception from each. For example,try { _([1, 2, 3, 4]).each((element) => { // Check your condition here if (element === 2) { throw new Error(); } // Do what you want with the elements here // ... }) } catch (e) { // Do nothing. }
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP