Brackets in Perl

For beginners, the usage of different types of brackets might be confusing. When to use curly brackets '{}', round brackets '()' or square brackets '[]'? After reading this post, you will be pretty clear.

Round brackets '()'


1. initialize array or hash
my %hash = ('key' => 'value' );
my @array = (1,4,5,6);

Curly brackets '{}'


1. access to hash values.
my %hash = ('key' => 'value' );
print $hash{'key'};

2. anonymous hash
my $hash = { 'key' => 'value' };

Square brackets '[]'


1. access elements of arrays.
my @array = (1,4,5,6);
print $array[3];

Perl reference

If you ever used c++, it will be easier to get the concept of reference in Perl.

Here is an example of reference to hash.
my %hash = (
    date => '08-10-2014',
);

my $ref = \%hash;

#note: In c++ we use '->' to access members of an object reference
print $ref->{date}."\n";

#note: In c++ we use '.' to access members of an object. 
print $hash{date}."\n";

Here are more examples
#reference to anonymous hash
my $ref1 = {
    date => '08-10-2014',
};

#reference to anonymous array
my $ref2 = [1,2,3,4,5];

#please note the way to access the index.
print $ref2->[4]."\n"; #output is 5.

Dereference:
#hash
%{$ref1} 

#array
@{$ref2}

html -- control image src using css

control image src using css. This will provide flexibility to change image on your web.
<style>
.imgClass{
 content:url("https://www.google.com/images/srpr/logo11w.png");
}
</style>

<img class="imgClass"/>

Output: the src url of img is a google logo.

Shell -- elements in file1 but not in file2

Given two files with multi records in each. Each line is a record.
Find the records in file1 but not in file2

sort file1 | uniq > newfile1;
sort file2 | uniq > newfile2;
comm -12 newfile1 newfile2 > common
More about getting common elements of two files.

Use 'uniq' to get the elements in file1 but not in file2
cat common newfile1 | sort | uniq

Shell -- common element of two files

Given two files with multi records in each. Each line is a record. Find the common elements of both files. Suppose the file names are file1 and file2. Firstly, make sure records in both files are unique.
sort file1 | uniq > newfile1;
sort file2 | uniq > newfile2;
From here, there are many ways to get the result:

1. use 'comm'
comm -12 newfile1 newfile2
2. use 'grep'
grep -f newfile1 newfile2
3. use 'uniq'
cat newfile1 newfile2 | sort | uniq -d

Perl configuration files

There are many ways to read data/configuration from file in perl.
Such like, parsing text file which is coma/tab delimited, reading from json file or other format using extra json package.

The following should be the simplest way, if you control the input.
#my.conf
return
{
    start_time => '10 pm'
};

Read configuration file which is perl format. Function 'do' will execute the input as a perl file and return the result.
#!/usr/bin/perl -w
use Data::Dumper;

my $hashref = do 'my.conf';
print Dumper($hashref);

Output:
$VAR1 = {
          'start_time' => '10 pm'
        };