Found 157 Articles for PERL

Returning Value from a Subroutine in Perl

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 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

Passing Hashes to Subroutines in Perl

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

1K+ 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 ) {       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

Passing Lists to Subroutines in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:55:55

617 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 Live Demo#!/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 ... Read More

Passing Arguments to a Subroutine in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:54:12

2K+ Views

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 More

Define and Call a Subroutine in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:51:13

166 Views

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 More

POSIX Function strftime() in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:41:03

9K+ Views

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 More

Epoch time in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:33:42

5K+ Views

You can use the time() function in Perl to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970.Example Live Demo#!/usr/local/bin/perl $epoc = time(); print "Number of seconds since Jan 1, 1970 - $epoc";OutputWhen the above code is executed, it produces the following result −Number of seconds since Jan 1, 1970 - 1361022130You can convert a given number of seconds into date and time string as follows −Example Live Demo#!/usr/local/bin/perl $datestring = localtime(); print "Current date and time $datestring"; $epoc = time(); $epoc = $epoc - 24 * 60 * 60; ... Read More

Format Date and Time in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:32:04

272 Views

You can use localtime() function in Perl to get a list of 9-elements and later you can use the printf() function to format date and time based on your requirements as follows −Example Live Demo#!/usr/local/bin/perl ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); printf("Time Format - HH:MM:SS"); printf("%02d:%02d:%02d", $hour, $min, $sec);OutputWhen the above code is executed, it produces the following result −Time Format - HH:MM:SS 06:58:52

GMT Time in Perl

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

488 Views

The function gmtime() in Perl works just like localtime() function but the returned values are localized for the standard Greenwich time zone. When called in list context, $isdst, the last value returned by gmtime, is always 0. There is no Daylight Saving Time in GMT.You should make a note on the fact that localtime() will return the current local time on the machine that runs the script and gmtime() will return the universal Greenwich Mean Time, or GMT (or UTC).Try the following example to print the current date and time but on GMT scale −Example Live Demo#!/usr/local/bin/perl $datestring = gmtime(); print ... Read More

Current Date and Time in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 06:28:47

321 Views

Let's start with localtime() function in Perl, which returns values for the current date and time if given no arguments. Following is the 9-element list returned by the localtime function while using in list context −sec, # seconds of minutes from 0 to 61 min, # minutes of hour from 0 to 59 hour, # hours of day from 0 to 24 mday, # day of month from 1 to 31 mon, # month of year from 0 to 11 year, # year since 1900 wday, # days since sunday yday, # days since January 1st isdst # hours of ... Read More

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