Programming Scripts Articles

Page 22 of 33

Passing Lists to Subroutines in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 974 Views

Because the @_ variable is an array in Perl, it can be used to supply lists to a subroutine. However, because of the way in which Perl accepts and parses lists and arrays, it can be difficult to extract the individual elements from @_. If you have to pass a list along with other scalar arguments, then make list as the last argument as shown below −Example#!/usr/bin/perl # Function definition sub PrintList {    my @list = @_;    print "Given list is @list"; } $a = 10; @b = (1, 2, 3, 4); # Function call with list parameter ...

Read More

Passing Hashes to Subroutines in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 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#!/usr/bin/perl # Function definition sub PrintHash {    my (%hash) = @_;    foreach my $key ( keys %hash ) {       my $value = $hash{$key};       print "$key : $value";    } } %hash = ('name' => 'Tom', 'age' => 19); # Function call with hash parameter PrintHash(%hash);OutputWhen the above program is executed, it produces the following result −name : Tom age : 19

Read More

Returning Value from a Subroutine in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 5K+ 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 any scalar but returning more than one array or hash normally causes them to lose their separate identities. So we will use references ( explained in the next chapter ) to return an array or hash from a function.ExampleLet's try the following example, which takes a list of numbers and ...

Read More

Private Variables in a Subroutine in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 651 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

Temporary Values via local() in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 323 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

State Variables via state() in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 974 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 −#!/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 of ...

Read More

Dereferencing in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ 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#!/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 @var ...

Read More

References to Functions in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 257 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#!/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 reference. ...

Read More

How to use Formats in Perl?

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 314 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

Create a Report Header using Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 346 Views

Sometime you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this using Perl. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable −Example#!/usr/bin/perl format EMPLOYEE = =================================== @

Read More
Showing 211–220 of 328 articles
« Prev 1 20 21 22 23 24 33 Next »
Advertisements