Mohd Mohtashim has Published 251 Articles

References to Functions in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 09:59:08

133 Views

While using Perl script, this might happen if you need to create a signal handler so you can produce a reference to a function by preceding that function name with \& and to dereference that reference you simply need to prefix reference variable using ampersand &. Following is an example ... Read More

Circular References in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:19:21

228 Views

A circular reference in Perl occurs when two references contain a reference to each other. You have to be careful while creating references otherwise a circular reference can lead to memory leaks. Following is an example −Example Live Demo#!/usr/bin/perl my $foo = 100; $foo = \$foo; print "Value of foo is ... Read More

Dereferencing in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:16:39

982 Views

Dereferencing in Perl returns the value from a reference point to the location. To dereference a reference simply use $, @ or % as a prefix of the reference variable depending on whether the reference is pointing to a scalar, array, or hash. Following is the example to explain the ... Read More

Create References in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:13:32

109 Views

A Perl reference is a scalar data type that holds the location of another value which could be scalar, arrays, or hashes. Because of its scalar nature, a reference can be used anywhere, a scalar can be used.It is easy to create a reference for any variable, subroutine or value ... Read More

Subroutine Call Context in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:07:35

179 Views

The context of a Perl subroutine or statement is defined as the type of return value that is expected. This allows you to use a single function that returns different values based on what the user is expecting to receive. For example, the following localtime() returns a string when it ... Read More

State Variables via state() in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:06:24

604 Views

There is another type of lexical variable in Perl, which is similar to private variables but they maintain their state and they do not get reinitialized upon multiple calls of the subroutines. These variables are defined using the state operator and available starting from Perl 5.9.4.ExampleLet's check the following example ... Read More

Temporary Values via local() in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:03:34

197 Views

The local is mostly used when the current value of a variable must be visible to called subroutines in Perl. A Perl local just gives temporary values to global (meaning package) variables. This is known as dynamic scoping. Lexical scoping is done with my, which works more like C's auto ... Read More

Private Variables in a Subroutine in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 07:01:28

388 Views

By default, all variables in Perl are global variables, which means they can be accessed from anywhere in the program. But you can create private variables called lexical variables at any time with the my operator.The my operator confines a variable to a particular region of code in which it ... Read More

Returning Value from a Subroutine in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 06:59:28

3K+ Views

You can return a value from Perl subroutine as you do in any other programming language. If you are not returning a value from a subroutine then whatever calculation is last performed in a subroutine is automatically also the return value.You can return arrays and hashes from the subroutine like ... Read More

Passing Hashes to Subroutines in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 06:57:13

2K+ Views

When you supply a hash to a Perl subroutine or operator that accepts a list, then the hash is automatically translated into a list of key/value pairs. For example −Example Live Demo#!/usr/bin/perl # Function definition sub PrintHash {    my (%hash) = @_;    foreach my $key ( keys %hash ) ... Read More

Advertisements