Tag Archives: Perl

Perl Code Snippets

Perl Search for Files and Directories # Use the file module to find filesuse File::Find;  # Current file can be accessed by $File::Find::name# Inside the sub you can apply any command for example# changing the file mode to 777find (sub …

Read more »

Perl DBI Connect Example

Here is a sample code snippet to connect and access an informix database using Perl DBI #!/usr/bin/perl # DBI moduleuse DBI ; # Database handler$dbh = DBI->connect("DBI:Informix:$db_name"); # Statement handler$sth = $dbh->prepare("$sql"); # Execute statement$sth->execute(); # For each row in the tablewhile (@row = $sth->fetchrow_array()){ …

Read more »

Perl Read File Line by Line

The following Perl code reads and replace some text in a file. Two code snippets are provided to read a small file and a large file: Reading a Small File #!/usr/bin/perl # Open file for readingopen(IN,"in.txt") or die "Cannot open file: …

Read more »

Date Validation using Regular Expression

Problem Write a regular expression in Perl to validate dates in the following format “mm/dd/yyyy”.  The month and day fields can be one or two digits. Code Here is a sample Perl code to do that #!/usr/bin/perl $date = " 1/1/2010 …

Read more »

Perl return undef

Question In Perl what is the difference between return and return undef Answer Using “return” alone returns an empty list in list context and undef in scalar context. On the other hand “return undef” always returns undef even in list …

Read more »

Perl Struct Example

Problem Provide example code in Perl to implement an array of C like structs Solution Use a hash table. The hash key is recommended to be a unique identifier and the hash value is a reference to an array. The …

Read more »

Perl Script to Parse Log File

Problem You have a log file on UNIX operating system. Each line in the log file contains an IP address in the 10th column where columns (or fields) are separated by a space. Write a Perl program to parse the …

Read more »

Page 1 of 3123