Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Scripts Articles
Page 6 of 33
Define and Call a Subroutine in Perl
The general form of a subroutine definition in Perl programming language is as follows −sub subroutine_name { body of the subroutine }The typical way of calling that Perl subroutine is as follows −subroutine_name( list of arguments );In versions of Perl before 5.0, the syntax for calling subroutines was slightly different as shown below. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes.&subroutine_name( list of arguments );Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before ...
Read MorePOSIX Function strftime() in Perl
You can use the POSIX function strftime() in Perl to format the date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent.SpecifierReplaced byExample%aAbbreviated weekday name *Thu%AFull weekday name *Thursday%bAbbreviated month name *Aug%BFull month name *August%cDate and time representation *Thu Aug 23 14:55:02 2001%CA year divided by 100 and truncated to integer (00-99)20%dDay of the month, zero-padded (01-31)23%DShort MM/DD/YY date, equivalent to %m/%d/%y08/23/01%eDay of the month, space-padded ( 1-31)23%FShort YYYY-MM-DD date, equivalent to %Y-%m-%d2001-08-23%gWeek-based year, last two digits (00-99)01%GWeek-based year2001%hAbbreviated month name * (same as %b)Aug%HAn hour in 24h ...
Read MorePassing Arguments to a Subroutine in Perl
You can pass various arguments to a Perl subroutine like you do in any other programming language and they can be accessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on.You can pass arrays and hashes as arguments like any scalar but passing 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 pass an array or hash.Let's try the following example, which takes a list ...
Read MorePassing Lists to Subroutines in Perl
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 MorePassing Hashes to Subroutines in Perl
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 MoreReturning Value from a Subroutine in Perl
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 MorePrivate Variables in a Subroutine in Perl
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 MoreTemporary Values via local() in Perl
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 MoreState Variables via state() in Perl
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 MoreDereferencing in Perl
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