5% assure discount from here only

Tuesday, July 5, 2011

(29) What is wantarray in perl?

Solution:

wantarray is a special keyword which returns a flag indicating which context your subroutine has been called in.

It will return one of three values.

true: If your subroutine has been called in list context
false: If your subroutine has been called in scalar context
undef: If your subroutine has been called in void context

For example:-

You have a subroutine named "display" and from that subroutine sometimes you want to return an array and sometimes a scalar.

So the syntax would be

sub display(){
return(wantarray() ? qw(A B C) : '1');
}

my @ab = display();
my $bd = display();

then output is

#@ab = (A B C);
and
#$bd = 1;

The output will be depend upon the left hand side
assignment. If you put an array (@ab) it will call
wantarray() else a scalar value will return.

Wednesday, June 1, 2011

(28) How to use ForkManager in perl code?

Solution:

This post is to demonstrate how to use Parallel::ForkManager, a simple and powerful Perl module available from CPAN that can be used to perform a series of operations in parallel within a single Perl script.

You need to instantiate the ForkManager with the "new" constructor. You must specify the maximum number of processes to be created. If you specify 0, then NO fork will be done; this is good for debugging purposes.

use strict;
use Parallel::ForkManager;

# set a number of maximum process you want to run
my $max_procs = 3;

my @names = qw( print online show );
# hash to resolve PID's back to child specific information

my $pm = new Parallel::ForkManager($max_procs);

# Setup a callback for when a child finishes up so we can
# get it's exit code
$pm->run_on_finish(
sub { my ($pid, $exit_code, $ident) = @_;
print "** $ident just got out of the pool ".
"with PID $pid and exit code: $exit_code\n";
}
);

$pm->run_on_start(
sub { my ($pid,$ident)=@_;
if($ident eq 'print'){
my $a = results(1);
print $a."\n";
}elsif($ident eq 'online'){
my $b = online(2);
print $b."\n";
}else{
my $c = show(3);
print $c."\n";
}
}
);

$pm->run_on_wait(
sub {
print "** Have to wait for one children ...\n"
},
0.5
);

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
$pm->finish($child); # pass an exit code to finish
}

print "Waiting for Children...\n";
$pm->wait_all_children;
print "Everybody is out of the pool!\n";

sub results{
my $process1 = shift;
return "Process number is $process1\n";
}

sub online{
my $process2 = shift;
return "Process number is $process2\n";
}

sub show{
my $process3 = shift;
return "Process number is $process3\n";
}

Do not use Parallel::ForkManager in an environment, where other child processes can affect the run of the main program, so using this module is not recommended in an environment where fork() / wait() is already used.

Tuesday, May 31, 2011

(27) How to use JSON in perl code?

Solution :

In this post i try to show how to use JSON with perl. JSON (Javascript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

In this example i had created 1 js (javascript) file named a.js, 2 .pl (perl) files (i) index.pl and (ii) json.pl.

In a.js

function checkuser(){
var a = document.getElementById('text1').value;
var b = document.getElementById('text2').value;
$('#loading').show("fast");
$.getJSON("/cgi-bin/json.pl?",{
'user': a,
'pass': b
},function (data) {
$.each(data.check, function (i, check) {
if(check[0] != 'invalid') {
$('#msg').html('');
$(check[1]).appendTo('#msg');

}else{
$('#msg').html('');
$(check[1]).appendTo('#msg');
}
$('#loading').hide("fast");
});
});
}

(i)In index.pl

#!/usr/bin/perl -w

use strict;
use warnings;

print "Content-type: text/html\n\n";

print qq~<html>
<head>
<title>JSON Example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script src="/a.js" type="text/javascript"></script>
</head>
<body>
<div align="center">
<div id="loading" style="background-color:#fff1a8;line-height:23px;display:none;width:15%"><img src="/loading.gif" class="loading_m2"> Loading... </img></div><br>
</div>
<div align="center">
<table>
<tr>
<td>UserName:</td>
<td><input type="text" id="text1" value="Lalit"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" id="text2" value="123456"></td>
</tr>
<tr align="center">
<td><input type="button" value="Submit" onclick="checkuser();"></td>
</tr>
</table>
</div>
<div align="center" id="msg"></div>
</body>
</html>~;

and
(ii)In json.pl

#!/usr/bin/perl -w

use CGI;
use JSON;

my $cgi = CGI->new;

my $username = $cgi->param("user");
my $password = $cgi->param("pass");
my ($return, $msg) = '';

if($username eq 'Lalit' && $password eq '123456'){
($return, $msg) = ('valid', 'Welcome Lalit');
}else{
($return, $msg) = ('invalid','You are not Authorized User');
}

my $json = JSON->new->utf8->space_after->encode({'check' =>
{'rawdata' => [$return, $msg]}});
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

This json script is used to check that a user is a valid user or not.

Monday, April 18, 2011

(26) How to print a matching line, one line immediately above it and one line immediately below?

Solution:

In perl this type of problems or requirement can be easily solved because of its versatility.

my $line='';

open(FILE,'/home/lalit/Desktop/test.txt') or die "Could not open file " . $!;

while (<FILE>){

    if($_ =~ /how are you/ig){
        print "\nprevious line of matching pattern =>" . $line;
        print "\nCurrent Line of matching pattern =>" . $_;
        print "\nNext Line of matching pattern =>" . scalar <FILE>."\n";
    }
    $line = $_;
}

close FILE;

(25) How to use Apache2::Ajax with perl code?

Solution:

In the previous post, no. 24, i tried to show how to use CGI::Ajax with perl code. Now i am going to use Apache2::Ajax which is mod_perl interface to CGI::Ajax.

This module require mod_perl2, CGI::Ajax, as well as a CGI.pm-compatible CGI module for supplying the param() and header() methods. If available, CGI::Apache2::Wrapper will be used, which is a minimal module that uses methods of mod_perl2 and Apache2::Request to provide these methods; if this is not available, CGI (version 2.93 or greater) will be used.

To use this module you have to make some changes in your httpd.conf, which is located at /etc/httpd/conf/.

To make the use of the below example, some steps need to be followed:-

(i) Create a directory cgi-bin2 in /var/www/.

(ii) Following change must be done your httpd.conf :-

PerlModule ModPerl::Registry
Alias /cgi-bin2/ /var/www/cgi-bin2/
<Location /cgi-bin2>
     SetHandler perl-script
     PerlResponseHandler ModPerl::Registry
     PerlOptions +GlobalRequest
     PerlSendHeader On
     PerlOptions +ParseHeaders
     Options +ExecCGI
     Order allow,deny
     Allow from all
</Location>

(iii) Create a file named with myajax.pl with the following code:

#!/usr/bin/perl -w

use strict;
use CGI;
use lib '/usr/lib/perl5/site_perl/5.8.8/';

use Apache2::MyAjaxApp;

my $r =shift;
Apache2::MyAjaxApp->handler($r);

(iv) Create a file named MyAjaxApp.pm in following location:

/usr/lib/perl5/site_perl/5.8.8/Apache2/

Put the following code into the file

package Apache2::MyAjaxApp;

use Apache2::Ajax;
use mod_perl2;
 
sub check {
    my $arg = shift;
    return ( $arg . " with some extra" );
}

sub Show_Form_sub {

    my $html = "";
    $html .= "<HTML>";
    $html .= "<HEAD>";
   
    $html .= <<EOT;
    </HEAD>
    <BODY>
    <FORM name="form">
    <INPUT type="text" id="inarg"
        onkeyup="tester(['inarg'],['output_div']); return true;">
    <hr>
    <div id="output_div"></div>
    </FORM>
    <br/><div id='pjxdebugrequest'></div><br/>
    </BODY>
    </HTML>
EOT

    return $html;
}

sub Show_Form_sub1 {

    my $html = "";
    $html .= "<HTML>";
    $html .= "<HEAD>";
   
    $html .= <<EOT;
    </HEAD>
    <BODY>
    Invalid
    </BODY>
    </HTML>
EOT

    return $html;
}
 
sub handler {

    my ($class,$r) = @_;
    my $ajax = Apache2::Ajax->new($r, 'tester' => \&check);

    $r->print($ajax->build_html('html' => \&Show_Form_sub));
    return Apache2::Const::OK;

}

1;

Restart your apache service and run in the browser with the following url:

http://localhost/cgi-bin2/myajax.pl

Monday, March 28, 2011

(24) How to use CGI::Ajax with perl code?

Solution:

A cpan module CGI::Ajax is available to make the use of Ajax in your web application. It is an object-oriented module that provides a unique mechanism for using perl code asynchronously from javascript- enhanced HTML pages.

A very simple example of CGI::Ajax :-

#!/usr/bin/perl -w

use strict;
use CGI;

use CGI;
use CGI::Ajax;

my $cgi = new CGI;

my $pjx = new CGI::Ajax('checkname' => \&checkname);
print $pjx->build_html( $cgi, \&ajax_html);


sub checkname{
    my $input = shift;
    my $output = $cgi->param('name');
    return $output;
}

sub header {

    my $header = qq~<HTML>
            <HEAD>
            </HEAD>
            <BODY>~;
    return $header;
}

sub ajax_html{
  
    my $html_header = header();
    my $html_footer = footer();
  
    my $html_body = qq~<input type="button" value="Click Me" onclick="checkname(['name__lalit'], ['lalitdiv']);">
        <div id="lalitdiv">Ajaxx</div>~;

    my $full_html = $html_header.$html_body.$html_footer;

    return $full_html;
}

sub footer{
  
    my $footer = qq~</BODY>
    </HTML>~;
    return $footer;

}

Friday, February 25, 2011

(23) What is difference between DBI and DBD?

Solution:

This is one of the most frequent question can be asked or can comes to mind when you worked on Perl.

DBI is database access library, whereas DBDs are "drivers" which are used by DBI
to access particular database (eg. there is one DBD for MySQL, another one for PostgreSQL etc).
You should use DBI rather than DBDs directly.

DBI is the interface. DBD are the implementations of that interface.

Wednesday, February 23, 2011

(22) What are the CGI Environment Variables?

Solution :

CGI Environment Variables plays an very important role in web application development. If you are using perl for developing web application then you must know about CGI Environment Variables. These are the series of some hidden values that the web server(apache or can be other) sends to every CGI program you run on browser. Your program can parse them and use the data they send. Environment variables are stored in a special hash named %ENV.

Most of the frequently used CGI Environment Variables are:

KeyValue
DOCUMENT_ROOT:The root directory of your server
CONTENT_LENGTH:The length, in bytes, of the input stream that is being passed through standard input.
PATH_INFO:The extra path information followin the script's path in the URL.
HTTP_COOKIE:The visitor's cookie, if one is set
HTTP_HOST:The hostname of the page being attempted
HTTP_REFERER:The URL of the page that called your program
HTTP_USER_AGENT:The browser type of the visitor
HTTPS:"on" if the program is being called through a secure server
PATH:The system path your server is running under
QUERY_STRING:The query string (see GET, below)
REMOTE_ADDR:The IP address of the visitor
REMOTE_HOST:The hostname of the visitor (if your server has reverse-name-lookups on; otherwise
this is the IP address again)
REMOTE_PORT:The port the visitor is connected to on the web server
REMOTE_USER:The visitor's username (for .htaccess-protected pages)
REQUEST_METHOD:GET or POST
REQUEST_URI:The interpreted pathname of the requested document or CGI (relative to the document
root)
SCRIPT_FILENAME:The full pathname of the current CGI
SCRIPT_NAME:The interpreted pathname of the current CGI (relative to the document root)
SERVER_ADMIN:The email address for your server's webmaster
SERVER_NAME:Your server's fully qualified domain name (e.g. www.cgi101.com)
SERVER_PORT:The port number your server is listening on
SERVER_SOFTWARE:The server software you're using (e.g. Apache 1.3)

Sunday, February 20, 2011

(21) Create a HTML Table based on the result of a database query.

Solution :

This type of requirement generally comes when u r working on website development. And generally we create this type of table by typing some html syntax of Table, TR, TH, TD etc. and than fulfill the requirement accordingly,

my $sth = $dbh->prepare( "select id, name, status from mytable where something = ... " );
$sth->execute() or die "Failed to query";
while (my $row = $sth->fetchrow_hashref) {

Here we fill the column with the database values.

}

$sth->finish;

Every time, whenever this type of requirement arise, same type of code implies. But HTML::Table makes things better by taking out most of the HTML drudgery, but you still need to loop through adding rows to your table.

This is where HTML::Table::FromDatabase comes in - it’s a subclass of HTML::Table which accepts an executed DBI statement handle, and automatically produces the table for you.

For instance:

my $sth = $dbh->prepare(
"select id, name, status from mytable where something = ..."
);
$sth->execute() or die "Failed to query";

my $table = HTML::Table::FromDatabase->new( -sth => $sth );
$table->print;

This is the very simple way to do it, You can also include more option as required. As HTML::Table::FromDatabase is a subclass of HTML::Table, all of HTML::Table’s options can still be used to control how the generated table appears, for example:

* -class => ‘classname’ to give the table a specific class to help you apply CSS styling
* -border => 1 to apply borders, -padding => 3 to set cell padding
* -evenrowclass and -oddrowclass if you want to have different styling for even and odd rows (e.g. alternating row backgrounds).

The full list of options can be found in the HTML::Table documentation.

Saturday, February 5, 2011

(20) How to get html content of a site?

Solution :

I love to do programming in PERL because of so many reasons, out of them one is to get content of a site is very easy. You can get any site content by using the following script.

For running this script the only thing you should have is cpan module named WWW::Mechanize which can be easily downloaded from search.cpan.org or can be installed from you machine by typing following command on terminal

$ su -
#Enter your root password

cpan WWW::Mechanize

#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;

### Enter url like (http://www.facebook.com) ###

print "Enter the URL\n";
my $url = <>;

my $mech = WWW::Mechanize->new;
$mech->get($url);

print $mech->content();

(19) Setting Up Perl Development Environment in Windows

Solution :

Basically PERL runs on Linux by default, but in Today's world peoples are using Microsoft Windows on Huge basis because of its functionality, features, GUI and of some many reasons. Here are some steps which are very useful in setting up PERL development in Microsoft Windows. We will use Windows environment through tutorial series. For your convenience, we will use all free development tools. You can also find such the same free tool in your platform such as Linux or Mac.

Download and install ActivePerl


You first need to download ActivePerl the latest version from http://www.activestate.com/activeperl/. The file we downloaded at the time of this tutorial being written was ActivePerl-5.10.0.1004-MSWin32-x86-287188.msi. You can also find the installation files for other systems and versions via following link

http://www.activestate.com/activeperl/downloads/

To install ActivePerl just double click on the installation file. It will guide you through steps with options. Just make your own choices and click next.

To check you installation successfully, you can open command line through Run window and type following command:

perl -version.

If you installed Perl successfully, you will see the version and copyright information.

Download and install Perl IDE: Open Perl IDE

Open Perl IDE stands for integrated development environment. It includes source code editor with syntax highlight, interpreter, and debugger tool. The IDE helps you a lot while develop Perl Program. We highly recommend that you should use Open Perl IDE because it is free, simple, intuitive and easy to use.

You can download the Open Perl IDE the latest version via following link: http://open-perl-ide.sourceforge.net/

With Open Perl IDE you don’t need to install to run it. First you unzip the downloaded file. (If you don’t have zip tool you can download a free tool called 7-ZIP from http://www.7-zip.org/ ). To launch the Open Perl IDE, you can open the unzip folder, find the file PerlIDE.exe, double click on it.

Now you are ready to start learning Perl!

Sunday, January 30, 2011

(18) Difference between Array and Hash?

Solution :

Both datatypes are used in PERL. Both of them are very useful in their respect.

Hash vs Array :-

(i) The basic differnce is the order maintain by both, array always keeps the order of elements inserted but hash will not do the same.

(ii) If data is at large level, than hash is best option as compare to array and at small level, traversing in array is much quicker than hash.

(iii) The symbol difference is there, array always nominated by @ whereas hash is denoting by % symbol.

(iv) The brackets differnce is there, array uses [] parenthesis whereas hash uses {} curly braces.

(v) The elements can be access from array by using index ($array[1]) whereas in hash use keys ($hash{'keys'}) for the same.

(vi) Declaration of both :

my @array = (1,2,3,4,5,6);

foreach my $i(@array){
print $array[$i];
}

my %hash = (1,2,3,4,5,6);

foreach my $keys(keys %hash){
print $keys." => ".$hash{$keys}."\n";
}

Thursday, January 27, 2011

(17) How to send MAIL from perl script?

Solution :

There are so many ways to send MAIL from perl script. Out of them the simplest perl script is here :

To run this the only thing is required the CPAN module Mail::Sendmail.

Perl, being perl, provides the programmer with more than one ways to do same thing, sending email included.
Simple platform independent e-mail from your perl script. Only requires Perl 5 and a network connection.
Mail::Sendmail contains mainly &sendmail, which takes a hash with the message to send and sends it. It is intended to be very easy to setup and use.

After installing the module, you have to run this command on terminal:

service sendmail restart

use Mail::Sendmail;

%mail = ( To => 'you@there.com',
From => 'me@here.com',
cc => 'if any', # optional
bcc => 'if any' # optional
Message => "This is a very short message"
);

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK. Log says:\n", $Mail::Sendmail::log;

(16) How to send SMS from perl script?

Solution :

To send sms you need a gateway that is willing to accept your message and dispatch to the respective networks. For this you need to register and stuff which is a tedious process.

It is very simple to send a SMS from perl script, there are so many ways to do that. Out of them i am going to post some of the ways for the same.

(1) Send SMS via 160By2

To use this script only 2 thing are required :

(i) CPAN module Net::SMS::160By2
(ii) An A/C with http://www.160by2.com/

use Net::SMS::160By2;

my $obj = Net::SMS::160By2->new($username, $password);

$obj->send_sms($msg, $to);

This is how you can send SMS to any mobile number in
India, Kuwait, UAE, Saudi, Singapore, Philippines & Malaysia at present.

(2) Send SMS via Way2SMS

To use this script only 2 thing are required :

(i) CPAN module Net::SMS::WAY2SMS;
(ii) An A/C with http://www.way2sms.com/

use Net::SMS::WAY2SMS;

my $s = Net::SMS::WAY2SMS->new(
'user' => 'xyz' ,
'password' => 'xyzpassword',
'mob'=>['98********', '9**********']
);

$s->send('Hello World');

Wednesday, January 26, 2011

(15) How to write a simple chat program in Perl script?

Solution :

Chat is a useful application that allows a number of people on the World Wide Web to talk to one another simultaneously. Now You can easily make chat using PERL script, there are so many options are available for this.

To make this possible, you must have following cpan module install on your system.

IO::Socket;
IO::Socket::INET;

Then create 2 .pl file, named recieve.pl and sender.pl, to recieve message and send message accordingly.

Put the following code in recieve.pl

my $reciever = new IO::Socket::INET (
        LocalHost => 'localhost',  # can be ip addr of machine if u r 
                                                      using another machine
        LocalPort => '9000',  # port should be same
        Proto => 'tcp',
        Listen => 1,
        Reuse => 1,
     ); die "Could not create socket: $!\n" unless $reciever;

my $new_sock = $reciever->accept();
while(<$new_sock>) {
    print $_;
}
close($reciever);

Put the following code in sender.pl

my $sender = new IO::Socket::INET (
        PeerAddr => 'localhost', # ip addr of machine if u r 
                                                      using another machine
        PeerPort => '9000', # port should be same
        Proto => 'tcp', );
    die "Could not create socket: $!\n" unless $sender;

print $sender "Hi Reciever ";
close($sender);


First run the reciever.pl on terminal 1 and than sender.pl on terminal 2.

Tuesday, January 25, 2011

(14) How to write a simple Perl Database Script?

Solution :

Perl packages enabling your Perl scripts to create and modify databases in standard Unix formats. Perl programmers, like programmers of any other language, typically need to store large amounts of data. Even
though Perl is an exceptional language for text processing, in many circumstances, a more structured
database-like format offers quicker access. In addition, it may also be necessary for a Perl script
to read or write a database that is also accessed through a C program.

Here is the very simple script of perl database connection. The database can be Oracle, Mysql etc etc.

use DBI;

$dbh = DBI->connect("dbi:mysql:xyz", $username, $password)
or die $DBI::errstr;

(13) How to fetch gmail content?

Solution :

Now you can access gmail contents via PERL scripting and can put content accordingly.

use WWW::Mechanize;
$mech = WWW::Mechanize->new;
$mech->get( "https://www.gmail.com/" );
$mech->content();
$mech->submit_form(
form_number => '1',
fields => {
Email => $username,
Passwd => $password,
}
);

my $inbox_url = "https://mail.google.com/mail/?ui=html&zy=e";
$mech->get($inbox_url);
$mailbox = $mech->content();

(12) How to make a hash remember, the order put elements into it?

Solution :

Ordinarily Perl does not guarantee the order of items in a hash to be in any predictable order.
If this is important you can easily add a "keep-in-order" behavior to a particular hash by "tying"
that hash to the Tie::IxHash module. Of course you must first use the module, then, once you tie
your hash to this module, the module will automatically add the desired behavio

Solution :

Use the Tie::IxHash from CPAN.

use Tie::IxHash;

tie my %myhash, 'Tie::IxHash';

(11) How to sort a hash by value/key?

Solution :

Sorting the output of a Perl hash by the hash key is fairly straightforward. It involves two Perl
functions, keys and sort, along with the good old foreach statement.

(i) sort a hash by key

my %hash = (
"a" => 1,
"c" => 3,
"d" => 4
);

my @keys = sort { $a cmp $b } keys %hash;

foreach my $key ( @keys ){
print $key, $hash{$key};
}

(ii) sort a hash by value

my %hash = (
"a" => 1,
"c" => 5,
"d" => 4
);

my @keys = sort { $hash{$a} <=> $hash{$b} } keys %hash;

foreach my $key ( @keys ){
print $key, $hash{$key};
}

(10) How to know how many entries are in a hash?

Solution :

my $key_count = keys %hash; # must be scalar context!

If you want to find out how many entries have a defined value than:

my $defined_value_count = grep { defined } values %hash;

(9) How to count the number of occurrences of a substring within a string?

Solution :

$string = "-9 55 48 -2 23 -76 4 14 -44";
while ($string =~ /-\d+/g) {
$count++;
}
print "There are $count negative numbers in the string";

(8) How to access or change a particular characters of a string?

Solution :

$string = "Just another Perl Hacker";
$first_char = substr( $string, 0, 1 ); # 'J'

To change part of a string, you can use the optional fourth argument
which is the replacement string.

substr( $string, 13, 4, "Perl 5.8.0" );

(7) How do I strip blank space from the beginning/end of a string?

Solution :

A substitution can do this for you. For a single line, you want to
replace all the leading or trailing whitespace with nothing. You
can do that with a pair of substitutions:

(i) To remove leading space
s/^\s+//;

(ii) To remove Trailing space
s/\s+$//;

(6) How do I reverse a string?

Solution :

my $string = "problems";
my $reversed = reverse $string;
print $reversed; # output is smelborp;

(5) Does Perl have a round() function?

Solution :

The POSIX module (part of the standard Perl distribution)
implements ceil(), floor().

use POSIX;

$ceil = ceil(3.5); # 4
$floor = floor(3.5); # 3

Monday, January 24, 2011

(4) How do I find the index of the last element in an array?

Solution :

my @a=(1,2,3,4,5,6);
my $lastindex=$#a;
print $lastindex;

(3) How to reverse array elements?

Solution :

my @a = ( 1 ,4,2, 9,10 );
my $length = scalar(@a);
my @b;
for(my $i=0;$i<=$length;$i++){ my $j = $a[$length - $i]; push(@b, $j); } print @b;

(2) How do i get the last element of array?

Solution:

my @array = (1,2,3,45,12,232,23);

print $array[$#array];

(1) How to remove duplicate element from an array?

Solution :

There are so many solution of that particular problem, but out of
them i had provided the 2 shortest solution overhere.
(1)
use List::MoreUtils qw(uniq);
my @A = (1,2, 2,4, 3, 4, 5) ;
my @B = uniq @A;
print @B;

(2)

my @A = (1,2, 2,4, 3, 4, 5) ;
my %hash = "";

foreach(@A){
$hash{$_} = $_;
}

my @b = keys %hash;
print @b;