Found 157 Articles for PERL

How to use Formats in Perl?

Mohd Mohtashim
Updated on 29-Nov-2019 10:04:54

119 Views

In order to invoke a format declaration in Perl Script, we would use the write keyword −write EMPLOYEE;The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function.select(STDOUT);We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as ... Read More

References to Functions in Perl

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

117 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 −Example Live Demo#!/usr/bin/perl # Function definition sub PrintHash {    my (%hash) = @_;    foreach $item (%hash) {       print "Item : $item";    } } %hash = ('name' => 'Tom', 'age' => 19); # Create a reference to above function. $cref = \&PrintHash; # Function call using ... Read More

How to Define a Format in Perl?

Mohd Mohtashim
Updated on 29-Nov-2019 10:01:08

56 Views

Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data.SyntaxFollowing is the syntax to define a Perl format −format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .Here FormatName represents the name of the format. The fieldline is the specific way, the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period.Next fieldline can contain any text or field holders. ... Read More

Circular References in Perl

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

214 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 : ", $$foo, "";OutputWhen the above program is executed, it produces the following result −Value of foo is : REF(0x9aae38)

Dereferencing in Perl

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

880 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 concept −Example Live Demo#!/usr/bin/perl $var = 10; # Now $r has reference to $var scalar. $r = \$var; # Print value available at the location stored in $r. print "Value of $var is : ", $$r, ""; @var = (1, 2, 3); # Now $r has reference to ... Read More

Create References in Perl

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

102 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 by prefixing it with a backslash as follows −$scalarref = \$foo; $arrayref = \@ARGV; $hashref = \%ENV; $coderef = \&handler; $globref = \*foo;You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash operator but a reference to an anonymous array can be created using the ... Read More

Subroutine Call Context in Perl

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

169 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 is called in scalar context, but it returns a list when it is called in list context.my $datestring = localtime( time );In this example, the value of $timestr is now a string made up of the current date and time, for example, Thu Nov 30 15:21:33 2000. Conversely −($sec, $min, ... Read More

State Variables via state() in Perl

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

552 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 to demonstrate the use of state variables − Live Demo#!/usr/bin/perl use feature 'state'; sub PrintCount {    state $count = 0; # initial value    print "Value of counter is $count";    $count++; } for (1..5) {    PrintCount(); }OutputWhen the above program is executed, it produces the following result −Value ... Read More

Temporary Values via local() in Perl

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

181 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 declarationsIf more than one variable or expression is given to local, they must be placed in parentheses. This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval.ExampleLet's check the following example ... Read More

Private Variables in a Subroutine in Perl

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

349 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 can be used and accessed. Outside that region, this variable cannot be used or accessed. This region is called its scope. Lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code ... Read More

Previous 1 ... 6 7 8 9 10 ... 16 Next
Advertisements