Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Sep 022010
 

Simple try catch block in Perl

Answer:

Perl don't have try/catch statement by default, but you can simulate using the following way:

eval {
    print 1/0;
};

if ($@) {
    print "Error message: $@";
};

When executed, it shows:

Error message: Illegal division by zero at test.pl line 7.

Aug 262010
 

Hash reference in Perl

Answer:

Hash reference is something like a pointer to a hash data structure in Perl

Example:

my %hash = (
        "a" => "1",
        "b" => "2",
);

my $hash_ref = \%hash;

$hash{"a"} = "A";

print $hash_ref->{"a"}; # print out "A" as change in  %hash  also reflected in its reference

Aug 222010
 

Convert array to object in PHP

Answer:

It is easy to cast an object to an array in PHP, using the following method.

<?php

    $obj = new stdClass(); 
    $obj->foo = "foo";
    $obj->bar = "bar";

    var_dump ( (array) $obj );

It will print out...

array(2) {
  ["foo"]=>
  string(3) "foo"
  ["bar"]=>
  string(3) "bar"
}
Aug 212010
 

A simple hash in Perl

Answer:

Hash is a dictionary like data structure in Perl. It allows you to direct access to a particular element inside a hash.

Example:

use strict;
use warnings;

my %hash = (
        "a" => "1",
        "b" => "2",
);

print $hash{"b"}; # print "2"