admin Site Admin
Joined: 18 Nov 2008 Posts: 32
|
Posted: Tue Nov 10, 2009 2:18 pm Post subject: simple yet powerful questions in PERL for a java ETL develop |
|
|
If you are a Java developer or Informatica or any ETL developer and looking for how to crack a PERL part, all you need to do is go through these simple 10 programs and you shall be able to do most of the stuff that is required for your job.
Perl is an acronym for "Practical Extraction and Report Language",
Perl was invented by Larry Wall in 1987.
sample # 1
Print “Hi”;
--------------------------
# pg1.pl
#"use warnings" - should be used at top of perl program. will display any warnings
#
use warnings;
print "Hi \n";
print "Hi ". "How are you ";
print<<EOF;
this is not a file
EOF
-----------------------------------------------
# pg2.pl
# this program explaining ord() function.
#
use warnings;
print "A # has ASCII value ". ord("#"). "\n";
-----------------------------------------------
# pg3.pl
# this program explaining how to declare variables.
use warnings;
my $name = "adoresoft";
print "this company name is $name \n";
{
my $name;
$name = 7;
print "name has a value of $name\n";
}
print "name has value of $name now \n";
-----------------------------------------------
# pg4.pl
# this program explaining how to take input to perl program.
use warnings;
print "currency converter. \nPlease enter the exchange rate: ";
my $rup = <STDIN>;
print " 75 dollars equals to ", $rup * 75, " Rupees\n";
print " 35 dollars equals to ", $rup * 35, " Rupees\n";
-----------------------------------------------
# pg5.pl
# this program explains arrays; @ symbal represents array declaration
use warnings;
print "explain list \n";
# quoted words
# how to declare an array
my @days;
my $days;
@days = qw (Monday Tuesday Wednesday Thrusday Friday Saturday Sunday);
$days = 31;
print @days , "\n";
print $days , "\n";
print "let see what happens below \n";
my $a = @days;
print "value of a is : $a \n";
my $b = $days[0];
print "value of b is : $b \n";
print $days[-3], "\n\n";
my $day;
for $day (@days) {
print $day, "\n";
}
print "\n\n";
print "teset: \n";
for (@days) {
print $_ , "\n";
}
print "\n";
-----------------------------------------------
# pg6.pl
# this program explains how to use file handlers.
# explains $_ a default variable.
# $! stores the latest error message
use warnings;
print "Explaining file handlers. \n";
open FILE, 'pg6.pl' or die $!;
my $linenum = 1;
while (<FILE>) {
print $linenum++;
print " :$_";
}
print " $! is messenger variable holding message of operation \n";
print $!;
--------------------------------------------------------------
#!/usr/bin/perl
# this is second program explaining how ARGV and Filters work
#use strict;
#use warnings;
my $ln = 1;
while (<>) {
print $ln++;
print ": $_";
}
print "this is second program explaining how ARGV and Filters work. \n";
print<<EOF
ARGV is a special file handler provided by perl. this reads
the names of files from command lineand opens them.
EOF
---------------------------------------------------------
#!/usr/bin/perl
# this is second program explaining how to write to a file.
#use strict;
#use warnings;
print "usage is pg8.pl source.txt target.txt \n";
my $source = shift @ARGV;
my $dest = shift @ARGV;
open IN , $source or die "can not read file $source $! \n";
open OUT, ">$dest" or die "can not wriite to file $! \n";
print "Copying $source to $dest \n";
while (<IN>) {
print OUT $_;
}
--------------------------------------------------------------
#!/usr/bin/perl
# this is second program explaining how to write to a file.
use strict;
use warnings;
my @array = (1..10);
foreach (@array) {
print $_++, "\n";
}
print "Array is now : @array \n";
---------------------------------------------
#!/usr/bin/perl
# this is second program explaining how to reverse.
use strict;
use warnings;
while (<STDIN>) {
chomp;
die "!end " unless $_;
my $rev1 = reverse $_;
print "$rev1 \n";
} |
|