Friday, June 10, 2011

Array to hash array example

use strict;
use warnings;

# for debugging
use Data::Dumper;

my @array = (
'ab cd ef',
'mn de fg',
);

my %hash;

for my $element (@array) {

if ($element =~ m{
^ # match the beginning of the
row
( \w{2} ) # match two characters
[ ] # match one space
(.*) # match the rest of the row
}mxs
) {
$hash{$1} = $2;
} else {
warn "error: incorrectly formatted array element: $element";
}

}

print Dumper \%hash;


OR


my %hash = map { split / ^ \S+ \K [ ]+ /x, $_ } @a;

See \K in perlre (5.10 or greater)

How can I skip lines when slurping a file in Perl

The basic idea of inserting, changing, or deleting a line from a text
file involves reading and printing the file to the point you want to
make the change, making the change, then reading and printing the rest
of the file. Perl doesn't provide random access to lines (especially
since the record input separator, $/, is mutable), although modules
such as Tie::File can fake it.

A Perl program to do these tasks takes the basic form of opening a
file, printing its lines, then closing the file:

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

while( <$in> )
       {
       print $out $_;
       }

   close $out;

Within that basic form, add the parts that you need to insert, change,
or delete lines.

To prepend lines to the beginning, print those lines before you enter
the loop that prints the existing lines.

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

print $out "# Add this line to the top\n"; # <--- HERE'S THE MAGIC

while( <$in> )
       {
       print $out $_;
       }

   close $out;

To change existing lines, insert the code to modify the lines inside
the while loop. In this case, the code finds all lowercased versions
of "perl" and uppercases them. The happens for every line, so be sure
that you're supposed to do that on every line!

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

print $out "# Add this line to the top\n";

while( <$in> )
       {
       s/\b(perl)\b/Perl/g;
       print $out $_;
       }

   close $out;

To change only a particular line, the input line number, $., is
useful. First read and print the lines up to the one you want to
change. Next, read the single line you want to change, change it, and
print it. After that, read the rest of the lines and print those:

while( <$in> )   # print the lines before the change
       {
       print $out $_;
       last if $. == 4; # line number before change
       }

my $line = <$in>;
$line =~ s/\b(perl)\b/Perl/g;
print $out $line;

while( <$in> )   # print the rest of the lines
       {
       print $out $_;
       }

To skip lines, use the looping controls. The next in this example
skips comment lines, and the last stops all processing once it
encounters either END or DATA.

while( <$in> )
       {
       next if /^\s+#/;             # skip comment lines
       last if /^__(END|DATA)__$/;  # stop at end of code marker
       print $out $_;
       }

Do the same sort of thing to delete a particular line by using next to
skip the lines you don't want to show up in the output. This example
skips every fifth line:

while( <$in> )
       {
       next unless $. % 5;
       print $out $_;
       }

If, for some odd reason, you really want to see the whole file at once
rather than processing line by line, you can slurp it in (as long as
you can fit the whole thing in memory!):

open my $in,  '<',  $file      or die "Can't read old file: $!"
open my $out, '>', "$file.new" or die "Can't write new file: $!";

my @lines = do { local $/; <$in> }; # slurp!

       # do your magic here

print $out @lines;

Modules such as File::Slurp and Tie::File can help with that too. If
you can, however, avoid reading the entire file at once. Perl won't
give that memory back to the operating system until the process
finishes.

You can also use Perl one-liners to modify a file in-place. The
following changes all 'Fred' to 'Barney' in inFile.txt, overwriting
the file with the new contents. With the -p switch, Perl wraps a while
loop around the code you specify with -e, and -i turns on in-place
editing. The current line is in $. With -p, Perl automatically prints
the value of $ at the end of the loop. See perlrun for more details.

perl -pi -e 's/Fred/Barney/' inFile.txt

To make a backup of inFile.txt, give -i a file extension to add:

perl -pi.bak -e 's/Fred/Barney/' inFile.txt

To change only the fifth line, you can add a test checking $., the
input line number, then only perform the operation when the test
passes:

perl -pi -e 's/Fred/Barney/ if $. == 5' inFile.txt

To add lines before a certain line, you can add a line (or lines!)
before Perl prints $_:

perl -pi -e 'print "Put before third line\n" if $. == 3' inFile.txt

You can even add a line to the beginning of a file, since the current
line prints at the end of the loop:

perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt

To insert a line after one already in the file, use the -n switch.
It's just like -p except that it doesn't print $_ at the end of the
loop, so you have to do that yourself. In this case, print $_ first,
then print the line that you want to add.

perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt

To delete lines, only print the ones that you want.

perl -ni -e 'print unless /d/' inFile.txt

       ... or ...

perl -pi -e 'next unless /d/' inFile.txt

File::Basename in perl Example

use File::Basename;

($name,$path,$suffix) = fileparse($fullname,@
suffixlist);

$name = fileparse($fullname,@suffixlist);
$basename = basename($fullname,@suffixlist);
$dirname  = dirname($fullname);

Perl Intervew Question


What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?
5
length() is a built-in prototyped as sub length($), and a scalar prototype silently changes aggregates into radically different forms. The scalar sense of a hash is false (0) if it's empty, otherwise it's a string representing the fullness of the buckets, like "18/32" or "39/64". The length of that string is likely to be 5. Likewise, `length(@a)' would be 2 if there were 37 elements in @a.

If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}?
The second is disallowed under `use strict "refs"'.
Dereferencing a string with *{"STR"} is disallowed under the refs stricture, although *{STR} would not be. This is similar in spirit to the way ${"STR"} is always the symbol table variable, while ${STR} may be the lexical variable. If it's not a bareword, you're playing with the symbol table in a particular dynamic fashion.
How do I do < fill-in-the-blank > for each element in an array?
#!/usr/bin/perl -w
@homeRunHitters = ('McGwire', 'Sosa', 'Maris', 'Ruth');
foreach (@homeRunHitters) {
print "$_ hit a lot of home runs in one year\n";
}

How do I replace every <TAB> character in a file with a comma?
perl -pi.bak -e 's/\t/,/g' myfile.txt
What is the easiest way to download the contents of a URL with Perl?
Once you have the libwww-perl library, LWP.pm installed, the code is this:
#!/usr/bin/perl
use LWP::Simple;
$url = get 'http://www.websitename.com/';

How to concatenate strings with Perl?
Method #1 - using Perl's dot operator:
$name = 'checkbook';
$filename = "/tmp/" . $name . ".tmp";

Method #2 - using Perl's join function
$name = "checkbook";
$filename = join "", "/tmp/", $name, ".tmp";

Method #3 - usual way of concatenating strings
$filename = "/tmp/${name}.tmp";

How do I read command-line arguments with Perl?
With Perl, command-line arguments are stored in the array named @ARGV.
$ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc.
$#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line is $#ARGV + 1.
Here's a simple program:
#!/usr/bin/perl
$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments.\n";
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
}
When would `local $_' in a function ruin your day?
When your caller was in the middle for a while(m//g) loop
The /g state on a global variable is not protected by running local on it. That'll teach you to stop using locals. Too bad $_ can't be the target of a my() -- yet.

What happens to objects lost in "unreachable" memory..... ?
What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), \$ap ]; }' ?

Their destructors are called when that interpreter thread shuts down.
When the interpreter exits, it first does an exhaustive search looking for anything that it allocated. This allows Perl to be used in embedded and multithreaded applications safely, and furthermore guarantees correctness of object code.

Assume that $ref refers to a scalar, an array, a hash or to some nested data structure. Explain the following statements:
$$ref; # returns a scalar
$$ref[0]; # returns the first element of that array
$ref- > [0]; # returns the first element of that array
@$ref; # returns the contents of that array, or number of elements, in scalar context
$&$ref; # returns the last index in that array
$ref- > [0][5]; # returns the sixth element in the first row
@{$ref- > {key}} # returns the contents of the array that

How do you match one letter in the current locale?
/[^\W_\d]/
We don't have full POSIX regexps, so you can't get at the isalpha() <ctype.h> macro save indirectly. You ask for one byte which is neither a non-alphanumunder, nor an under, nor a numeric. That leaves just the alphas, which is what you want.
Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
They are not identical. There are several differences between using single quotes and double quotes for strings.
1. The double-quoted string will perform variable interpolation on its contents. That is, any variable references inside the quotes will be replaced by the actual values.
2. The single-quoted string will print just like it is. It doesn't care the dollar signs.
3. The double-quoted string can contain the escape characters like newline, tab, carraige return, etc.
4. The single-quoted string can contain the escape sequences, like single quote, backward slash, etc.
How many ways can we express string in Perl?
Many. For example 'this is a string' can be expressed in:
"this is a string"
qq/this is a string like double-quoted string/
qq^this is a string like double-quoted string^
q/this is a string/
q&this is a string&
q(this is a string)
How do you give functions private variables that retain their values between calls?
Create a scope surrounding that sub that contains lexicals.
Only lexical variables are truly private, and they will persist even when their block exits if something still cares about them. Thus:
{ my $i = 0; sub next_i { $i++ } sub last_i { --$i } }
creates two functions that share a private variable. The $i variable will not be deallocated when its block goes away because next_i and last_i need to be able to access it.
What is meant by die in a perl program?
Don’t proceed with the rest of the script of the pervious defined condition is NOT met.
What is the use of require and what does it
require is a call to an external program/condition, that has to be met before the scrcipt/program can continue.
What does this mean ‘$^0′
$^ - Holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle’s name with _TOP appended to it.
What is meant by ‘chomp’?
chomp is used to eliminate the new line character. It can used in many different scenarios.
For ex: excuteScript.pl firstArgument.
$firstArg = $ARGV[0];
chomp $firstArg; –> to get rid of the carrige return.
What is meant by a ‘pack’ in perl?
Pack converts a list into a binary representation
Takes an array or list of values and packs it into a binary structure, returning the string containing the structure
What does this symbol mean ‘->’
In Perl it is an infix dereference operator. The if the rhs is an array subscript, or a hash key, or a subroutine, then the lhs must be a reference, can also be used as method invocation: invocant->method
reference the method of a module.
What are the benefits of having global and local variables?
Global variables can be called upon any where in the script. Local variables are not valid outside the code blocks they are created in.
What is a subroutine?
A subroutine is like a function … called upon to execute a task.
What does the word ‘&my variable’ mean? What does the symbol ‘&’ means? What’s purpose of it?
&myvariable is calling a sub-routine. & is used to identify a sub-routine.
What does $_ means?
Default variable in PERL
What interface used in PERL to connect to database. How do you connect to database in perl
DBI.There is DBI module.use DBI;my $dbh = DBI->connect(’dbi:Oracle:orcl’, ‘username’, ‘password’,)
When do you not use PERL for a project?
There’s a lot of text processing
Web-based applications
Fast/expidient development
Shell scripts grow into libraries
Heavy Data manipulation (auditing, accounting, checking etc… backend processing)
Data extraction
transform loading (database etc.)
System admin etc…
Name an instance you used in CPAN module
CGI, DBI are very common packages used from CPAN.
Difference between for & foreach,exec & system?
There is no difference between for and foreach. exec runs the given process, switches to its name and never returns while system forks off the given process, waits for it to complete and then returns.
What’s the purpose of -w.strict,-T?
-w option enables warning.use strict pragma is used then you should declare variables before there use.
What is the use of “STDERR()”?
How to Connect with SqlServer from perl and how to display database table info?
There is a module in perl named DBI - Database independent interface which will be used to connect to any database by using same code. Along with DBI we should use database specific module here it is SQL server. for MSaccess it is DBD::ODBC, for MySQL it is DBD::mysql driver, for integrating oracle with perl use DBD::oracle driver is used. IIy for SQL server there are avilabale many custom defined ppm( perl package manager) like Win32::ODBC, mssql::oleDB etc.so, together with DBI, mssql::oleDB we can access SQL server database from perl. the commands to access database is same for any database.
What’s the significance of @ISA, @EXPORT @EXPORT_OK %EXPORT_TAGS list & hashes in a perl package?
@ISA -> each package has its own @ISA array. this array keep track of classes it is inheriting.
package child;
@ISA=( parentclass);
@EXPORT this array stores the subroutins to be exported from a module.
@EXPORT_OK this array stores the subroutins to be exported only on request.
What is difference between “Use” and “require”. In which case should “Use” be used and not “Require”?
Use :
1. The method is used only for the modules(only to include .pm type file)
2. The included objects are varified at the time of compilation.
3. No Need to give file extension.
Require:
1. The method is used for both libraries and modules.
2. The included objects are varified at the run time.
3. Need to give file Extension.
What is the difference between “my” and “local” variable scope declarations. ?
The variables declared with my() are visible only within the scope of the block which names them. They are not visible outside of this block, not even in routines or blocks that it calls. local() variables, on the other hand, are visible to routines that are called from the block where they are declared. Neither is visible after the end (the final closing curly brace) of the block at all.
What is a static function?
A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said tohave global, or external, scope. If the function or variable is not visible outside of the current source file, itis said to have local, or static, scope.
also if a variable is declared to be static, then its value doesn’t change within that function.
How do I find which modules are installed on my system?

You can use the ExtUtils::Installed module to show all installed distributions, although it can take awhile to do its magic. The standard library which comes with Perl just shows up as "Perl" (although you can get those with Module::CoreList).

use ExtUtils::Installed;

my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();

If you want a list of all of the Perl module filenames, you can use File::Find::Rule.

use File::Find::Rule;

my @files = File::Find::Rule->file()->name( '*.pm' )->in( @INC );

If you do not have that module, you can do the same thing with File::Find which is part of the standard library.

use File::Find;
my @files;

find(
sub {
push @files, $File::Find::name
if -f $File::Find::name && /\.pm$/
},

@INC
);

print join "\n", @files;

If you simply need to quickly check to see if a module is available, you can check for its documentation. If you can read the documentation the module is most likely installed. If you cannot read the documentation, the module might not have any (in rare cases).

prompt% perldoc Module::Name

You can also try to include the module in a one-liner to see if perl finds it.

perl -MModule::Name -e1

How can I compare two dates and find the difference?
You could just store all your dates as a number and then subtract. Life isn't always that simple though. If you want to work with formatted dates, the Date::Manip, Date::Calc, or DateTime modules can help you.
How do I remove consecutive pairs of characters?
You can use the substitution operator to find pairs of characters (or runs of characters) and replace them with a single instance. In this substitution, we find a character in (.). The memory parentheses store the matched character in the back-reference \1 and we use that to require that the same thing immediately follow it. We replace that part of the string with the character in $1.

s/(.)\1/$1/g;

We can also use the transliteration operator, tr///. In this example, the search list side of our tr/// contains nothing, but the c option complements that so it contains everything. The replacement list also contains nothing, so the transliteration is almost a no-op since it won't do any replacements (or more exactly, replace the character with itself). However, the s option squashes duplicated and consecutive characters in the string so a character does not show up next to itself

my $str = 'Haarlem'; # in the Netherlands
$str =~ tr///cs; # Now Harlem, like in New York


How can I access or change N characters of a string?


You can access the first characters of a string with substr(). To get the first character, for example, start at position 0 and grab the string of length 1.

$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" );

You can also use substr() as an lvalue.

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


How do I change the Nth occurrence of something?

You have to keep track of N yourself. For example, let's say you want to change the fifth occurrence of "whoever" or "whomever" into "whosoever" or "whomsoever", case insensitively. These all assume that $_ contains the string to be altered.

$count = 0;
s{((whom?)ever)}{
++$count == 5 # is it the 5th?
? "${2}soever" # yes, swap
: $1 # renege and leave it there
}ige;

In the more general case, you can use the /g modifier in a while loop, keeping count of matches.

$WANT = 3;
$count = 0;
$_ = "One fish two fish red fish blue fish";
while (/(\w+)\s+fish\b/gi) {
if (++$count == $WANT) {
print "The third fish is a $1 one.\n";
}
}

That prints out: "The third fish is a red one." You can also use a repetition count and repeated pattern like this:

/(?:\w+\s+fish\s+){2}(\w+)\s+fish/i;


How can I count the number of occurrences of a substring within a string?

There are a number of ways, with varying efficiency. If you want a count of a certain single character (X) within a string, you can use the tr/// function like so:

$string = "ThisXlineXhasXsomeXx'sXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";

This is fine if you are just looking for a single character. However, if you are trying to count multiple character substrings within a larger string, tr/// won't work. What you can do is wrap a while() loop around a global pattern match. For example, let's count negative integers:

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

Another version uses a global match in list context, then assigns the result to a scalar, producing a count of the number of matches.

$count = () = $string =~ /-\d+/g;


How do I capitalize all the words on one line?

To make the first letter of each word upper case:

$line =~ s/\b(\w)/\U$1/g;

This has the strange effect of turning "don't do it" into "Don'T Do It". Sometimes you might want this. Other times you might need a more thorough solution (Suggested by brian d foy):

$string =~ s/ (
(^\w) #at the beginning of the line
| # or
(\s\w) #preceded by whitespace
)
/\U$1/xg;

$string =~ s/([\w']+)/\u\L$1/g;

To make the whole line upper case:

$line = uc($line);

To force each word to be lower case, with the first letter upper case:

$line =~ s/(\w+)/\u\L$1/g;


What is the difference between a list and an array?

An array has a changeable length. A list does not. An array is something you can push or pop, while a list is a set of values. Some people make the distinction that a list is a value while an array is a variable. Subroutines are passed and return lists, you put things into list context, you initialize arrays with lists, and you foreach() across a list. @ variables are arrays, anonymous arrays are arrays, arrays in scalar context behave like the number of elements in them, subroutines access their arguments through the array @_, and push/pop/shift only work on arrays.

As a side note, there's no such thing as a list in scalar context. When you say

$scalar = (2, 5, 7, 9);

you're using the comma operator in scalar context, so it uses the scalar comma operator. There never was a list there at all! This causes the last value to be returned: 9.


Why does defined() return true on empty arrays and hashes?

The short story is that you should probably only use defined on scalars or functions, not on aggregates (arrays and hashes).


How do I sort a hash (optionally by value instead of key)?

To sort a hash, start with the keys. In this example, we give the list of keys to the sort function which then compares them ASCIIbetically (which might be affected by your locale settings). The output list has the keys in ASCIIbetical order. Once we have the keys, we can go through them to create a report which lists the keys in ASCIIbetical order.

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

foreach my $key ( @keys )
{
printf "%-20s %6d\n", $key, $hash{$value};
}

We could get more fancy in the sort() block though. Instead of comparing the keys, we can compute a value with them and use that value as the comparison.

For instance, to make our report order case-insensitive, we use the \L sequence in a double-quoted string to make everything lowercase. The sort() block then compares the lowercased values to determine in which order to put the keys.

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

Note: if the computation is expensive or the hash has many elements, you may want to look at the Schwartzian Transform to cache the computation results.

If we want to sort by the hash value instead, we use the hash key to look it up. We still get out a list of keys, but this time they are ordered by their value.

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

From there we can get more complex. If the hash values are the same, we can provide a secondary sort on the hash key.

my @keys = sort {
$hash{$a} <=> $hash{$b}
or
"\L$a" cmp "\L$b"
} keys %hash;


How can I make my hash remember the order I put elements into it?

Use the Tie::IxHash from CPAN.

use Tie::IxHash;

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

for (my $i=0; $i<20; $i++) {
$myhash{$i} = 2*$i;
}

my @keys = keys %myhash;
# @keys = (0,1,2,3,...)


What's the difference between "delete" and "undef" with hashes?

Hashes contain pairs of scalars: the first is the key, the second is the value. The key will be coerced to a string, although the value can be any kind of scalar: string, number, or reference. If a key $key is present in %hash, exists($hash{$key}) will return true. The value for a given key can be undef, in which case $hash{$key} will be undef while exists $hash{$key} will return true. This corresponds to ($key, undef) being in the hash.

Pictures help... here's the %hash table:

keys values
+------+------+
| a | 3 |
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

And these conditions hold

$hash{'a'} is true
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is true
exists $hash{'a'} is true (Perl 5 only)
grep ($_ eq 'a', keys %hash) is true

If you now say

undef $hash{'a'}

your table now reads:

keys values
+------+------+
| a | undef|
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

and these conditions now hold; changes in caps:

$hash{'a'} is FALSE
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is FALSE
exists $hash{'a'} is true (Perl 5 only)
grep ($_ eq 'a', keys %hash) is true

Notice the last two: you have an undef value, but a defined key!

Now, consider this:

delete $hash{'a'}

your table now reads:

keys values
+------+------+
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

and these conditions now hold; changes in caps:

$hash{'a'} is false
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is false
exists $hash{'a'} is FALSE (Perl 5 only)
grep ($_ eq 'a', keys %hash) is FALSE

See, the whole entry is gone!

How do I send e-mail from a Perl/CGI program on a Unix system?
Sending e-mail from a Perl/CGI program on a Unix computer system is usually pretty simple. Most Perl programs directly invoke the Unix sendmail program. We'll go through a quick example here.
Assuming that you've already have e-mail information you need, such as the send-to address and subject, you can use these next steps to generate and send the e-mail message:
# the rest of your program is up here ...
open(MAIL, "|/usr/lib/sendmail -t");
print MAIL "To: $sendToAddress\n";
print MAIL "From: $myEmailAddress\n";
print MAIL "Subject: $subject\n";
print MAIL "This is the message body.\n";
print MAIL "Put your message here in the body.\n";
close (MAIL);
How to read from a pipeline with Perl
Example 1:
 
 To run the date command from a Perl program, and read the output
 of the command, all you need are a few lines of code like this:
 
    open(DATE, "date|");
    $theDate = <DATE>;
    close(DATE);
 
 The open() function runs the external date command, then opens
 a file handle DATE to the output of the date command.
 
 Next, the output of the date command is read into
 the variable $theDate through the file handle DATE.
  
 Example 2:
 
 The following code runs the "ps -f" command, and reads the output:
 
    open(PS_F, "ps -f|");
    while (<PS_F>) {
       ($uid,$pid,$ppid,$restOfLine) = split;
       # do whatever I want with the variables here ...
    }
    close(PS_F);
 

Why is it hard to call this function: sub y { "because" }
Because y is a kind of quoting operator.
The y/// operator is the sed-savvy synonym for tr///. That means y(3) would be like tr(), which would be looking for a second string, as in tr/a-z/A-Z/, tr(a-z)(A-Z), or tr[a-z][A-Z].
 
 What does `$result = f() .. g()' really return?    
False so long as f() returns false, after which it returns true until g() returns true, and then starts the cycle again.
This is scalar not list context, so we have the bistable flip-flop range operator famous in parsing of mail messages, as in `$in_body = /^$/ .. eof()'. Except for the first time f() returns true, g() is entirely ignored, and f() will be ignored while g() later when g() is evaluated. Double dot is the inclusive range operator, f() and g() will both be evaluated on the same record. If you don't want that to happen, the exclusive range operator, triple dots, can be used instead. For extra credit, describe this:
$bingo = ( a() .. b() ) ... ( c() .. d() );
Why does Perl not have overloaded functions?
Because you can inspect the argument count, return context, and object types all by yourself.
In Perl, the number of arguments is trivially available to a function via the scalar sense of @_, the return context via wantarray(), and the types of the arguments via ref() if they're references and simple pattern matching like /^\d+$/ otherwise. In languages like C++ where you can't do this, you simply must resort to overloading of functions.

What does read() return at end of file?
0
A defined (but false) 0 value is the proper indication of the end of file for read() and sysread().
What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)
$cur->new()->{LINK}
The indirect object syntax only has a single token lookahead. That means if new() is a method, it only grabs the very next token, not the entire following expression.
This is why `new $obj[23] arg' does't work, as well as why `print $fh[23] "stuff\n"' does't work. Mixing notations between the OO and IO notations is perilous. If you always use arrow syntax for method calls, and nothing else, you'll not be surprised.

How do I sort a hash by the hash value?
Here's a program that prints the contents
 of the grades hash, sorted numerically by the hash value:
 
 #!/usr/bin/perl -w
 
 # Help sort a hash by the hash 'value', not the 'key'.    
              to highest).
 sub hashValueAscendingNum {
    $grades{$a} <=> $grades{$b};
 }
 
 
 
 # Help sort a hash by the hash 'value', not the 'key'.    
 # Values are returned in descending numeric order         
 # (highest to lowest).
 sub hashValueDescendingNum {
    $grades{$b} <=> $grades{$a};
 }
 
 
 %grades = (
        student1 => 90,
        student2 => 75,
        student3 => 96,
        student4 => 55,
        student5 => 76,
 );
 
 print "\n\tGRADES IN ASCENDING NUMERIC ORDER:\n";
 foreach $key (sort hashValueAscendingNum (keys(%grades))) {
    print "\t\t$grades{$key} \t\t $key\n";
 }
 
 print "\n\tGRADES IN DESCENDING NUMERIC ORDER:\n";
 foreach $key (sort hashValueDescendingNum (keys(%grades))) {
    print "\t\t$grades{$key} \t\t $key\n";
 }
 


How to read file into hash array ?
 
 open(IN, "<name_file")
   or die "Couldn't open file for processing: $!";
 while (<IN>) {
   chomp;
   $hash_table{$_} = 0;
 }
 close IN;
 

How do you find the length of an array?
$@array

What value is returned by a lone `return;' statement?
The undefined value in scalar context, and the empty list value () in list context.
This way functions that wish to return failure can just use a simple return without worrying about the context in which they were called.

What's the difference between /^Foo/s and /^Foo/?
The second would match Foo other than at the start of the record if $* were set.
The deprecated $* flag does double duty, filling the roles of both /s and /m. By using /s, you suppress any settings of that spooky variable, and force your carets and dollars to match only at the ends of the string and not at ends of line as well -- just as they would if $* weren't set at all.
Does Perl have reference type?
Yes. Perl can make a scalar or hash type reference by using backslash operator.
For example
$str = "here we go"; # a scalar variable
$strref = \$str; # a reference to a scalar

@array = (1..10); # an array
$arrayref = \@array; # a reference to an array
Note that the reference itself is a scalar.


 print "$_ = $hash_table{$_}\n" foreach keys %hash_table;

How to dereference a reference?
There are a number of ways to dereference a reference.
Using two dollar signs to dereference a scalar.
$original = $$strref;
Using @ sign to dereference an array.
@list = @$arrayref;
Similar for hashes.
How do I generate a list of all .html files in a directory?
Here's a snippet of code that just prints a listing of every file in the current directory that ends with the extension .html:
#!/usr/bin/perl -w
opendir(DIR, ".");
@files = grep(/\.html$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
print "$file\n";
}

What is Perl one-liner?
There are two ways a Perl script can be run:
--from a command line, called one-liner, that means you type and execute immediately on the command line. You'll need the -e option to start like "C:\ %gt perl -e "print \"Hello\";". One-liner doesn't mean one Perl statement. One-liner may contain many statements in one line.
--from a script file, called Perl program.

Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?
${var} is the lexical variable $var, and ${"var"} is the dynamic variable $var.
Note that because the second is a symbol table lookup, it is disallowed under `use strict "refs"'. The words global, local, package, symbol table, and dynamic all refer to the kind of variables that local() affects, whereas the other sort, those governed by my(), are variously knows as private, lexical, or scoped variable.
What happens when you return a reference to a private variable?
Perl keeps track of your variables, whether dynamic or otherwise, and doesn't free things before you're done using them.

How to turn on Perl warnings? Why is that important?
Perl is very forgiving of strange and sometimes wrong code, which can mean hours spent searching for bugs and weird results. Turning on warnings helps uncover common mistakes and strange places and save a lot of debugging time in the long run. There are various ways of turning on Perl warnings:
  • For Perl one-liner, use -w option on the command line.
  • On Unix or Windows, use the -w option in the shebang line (The first # line in the script). Note: Windows Perl interpreter may not require it.
  • For other systems, choose compiler warnings, or check compiler documentation.

What are scalar data and scalar variables?
Perl has a flexible concept of data types. Scalar means a single thing, like a number or string. So the Java concept of int, float, double and string equals to Perl\'s scalar in concept and the numbers and strings are exchangeable. Scalar variable is a Perl variable that is used to store scalar data. It uses a dollar sign $ and followed by one or more aphanumeric characters or underscores. It is case sensitive.

Why should I use the -w argument with my Perl programs?
Many Perl developers use the -w option of the interpreter, especially during the development stages of an application. This warning option turns on many warning messages that can help you understand and debug your applications.
To use this option on Unix systems, just include it on the first line of the program, like this:
#!/usr/bin/perl -w
If you develop Perl apps on a DOS/Windows computer, and you're creating a program named myApp.pl, you can turn on the warning messages when you run your program like this:
perl -w myApp.pl

Assuming $_ contains HTML, which of the following substitutions will remove all tags in it?
1.s/<.*>//g;
2.s/<.*?>//gs;
3.s/<\/?[A-Z]\w*(?:\s+[A-Z]\w*(?:\s*=\s*(?:(["']).*?\1|[\w-.]+))?)*\s*>//gsix;
You can't do that.
If it weren't for HTML comments, improperly formatted HTML, and tags with interesting data like < SCRIPT >, you could do this. Alas, you cannot. It takes a lot more smarts, and quite frankly, a real parser.

I want users send data by formmail but when they send nothing or call it from web site they will see error.
codes in PHP like this:
if (isset($HTTP_POST_VARS)){
..........
}
else{
echo ("error lalalalal")
}
How it will look in perl?
In php it will be like
if (isset($HTTP_POST_VARS)){
....
}
In perl, tried this.
if ($ENV{'REQUEST_METHOD'} eq 'POST'){
.....
}

What is the output of the following Perl program?
1 $p1 = "prog1.java";
2 $p1 =~ s/(.*)\.java/$1.cpp/;
3 print "$p1\n";
prog1.cpp

Why aren't Perl's patterns regular expressions?
Because Perl patterns have backreferences.
A regular expression by definition must be able to determine the next state in the finite automaton without requiring any extra memory to keep around previous state. A pattern /([ab]+)c\1/ requires the state machine to remember old states, and thus disqualifies such patterns as being regular expressions in the classic sense of the term.
What does Perl do if you try to exploit the execve(2) race involving setuid scripts?
Sends mail to root and exits.
It has been said that all programs advance to the point of being able to automatically read mail. While not quite at that point (well, without having a module loaded), Perl does at least automatically send it.
How do I do < fill-in-the-blank > for each element in a hash?
Here's a simple technique to process each element in a hash:
 
 #!/usr/bin/perl -w
 
 %days = (
         'Sun' =>'Sunday',
         'Mon' => 'Monday',
         'Tue' => 'Tuesday',
         'Wed' => 'Wednesday',
         'Thu' => 'Thursday',
         'Fri' => 'Friday',
         'Sat' => 'Saturday' );
 
 foreach $key (sort keys %days) {
   print "The long name for $key is $days{$key}.\n";
 

How do I sort a hash by the hash key?
Suppose we have a class of five students.
 Their names are kim, al, rocky, chrisy, and jane.
 
 Here's a test program that prints the contents
 of the grades hash, sorted by student name:
 
 #!/usr/bin/perl -w
 
 %grades = (
        kim       => 96,
        al        => 63,
        rocky     => 87,
        chrisy    => 96,
        jane      => 79,
 );
 
 print "\n\tGRADES SORTED BY STUDENT NAME:\n";
 foreach $key (sort (keys(%grades))) {
    print "\t\t$key \t\t$grades{$key}\n";
 }
 
 
 The output of this program looks like this:
 
 
        GRADES SORTED BY STUDENT NAME:
               al             63
               chrisy         96
               jane           79
               kim            96
               rocky          87
 
 
 
 }
 
}
 

How do you print out the next line from a filehandle with all its bytes reversed?
print scalar reverse scalar <FH>
Surprisingly enough, you have to put both the reverse and the <FH> into scalar context separately for this to work.
Why do you use Perl?
  • Perl is a powerful free interpreter.
  • Perl is portable, flexible and easy to learn.

How do I set environment variables in Perl programs?
you can just do something like this:
$ENV{'PATH'} = '...';
As you may remember, "%ENV" is a special hash in Perl that contains the value of all your environment variables.
Because %ENV is a hash, you can set environment variables just as you'd set the value of any Perl hash variable. Here's how you can set your PATH variable to make sure the following four directories are in your path::

$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/home/yourname/bin';
Which of these is a difference between C++ and Perl?
Perl can have objects whose data cannot be accessed outside its class, but C++ cannot.
Perl can use closures with unreachable private data as objects, and C++ doesn't support closures. Furthermore, C++ does support pointer arithmetic via `int *ip = (int*)&object', allowing you do look all over the object. Perl doesn't have pointer arithmetic. It also doesn't allow `#define private public' to change access rights to foreign objects. On the other hand, once you start poking around in /dev/mem, no one is safe.

How to open and read data files with Perl
Data files are opened in Perl using the open() function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.
As an example, suppose you need to read some data from a file named "checkbook.txt". Here's a simple open statement that opens the checkbook file for read access: open (CHECKBOOK, "checkbook.txt"); In this example, the name "CHECKBOOK" is the file handle that you'll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named "CHECKBOOK".
Now that we've opened the checkbook file, we'd like to be able to read what's in it. Here's how to read one line of data from the checkbook file:
$record = < CHECKBOOK > ;
After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The "<>" symbol is called the line reading operator.
To print every record of information from the checkbook file

open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!";
while ($record = < CHECKBOOK >) {
print $record;
}
close(CHECKBOOK);

How do I do fill_in_the_blank for each file in a directory?
Here's code that just prints a listing of every file in the current directory:
#!/usr/bin/perl -w
opendir(DIR, ".");
@files = readdir(DIR);
closedir(DIR);
foreach $file (@files) {
print "$file\n";
}
Perl is a high-level programming language with an eclectic heritage written by Larry Wall and a cast of thousands. It derives from the ubiquitous C programming language and to a lesser extent from sed, awk, the Unix shell, and at least a dozen other tools and languages. Perl's process, file, and text manipulation facilities make it particularly well-suited for tasks involving quick prototyping, system utilities, software tools, system management tasks, database access, graphical programming, networking, and world wide web programming. These strengths make it especially popular with system administrators and CGI script authors, but mathematicians, geneticists, journalists, and even managers also use Perl. Maybe you should, too.

Purpose of first line #!usr/bin/perl in a Perl Program

When you run a program from your command line, you have certain information that is passed to the shell without you thinking about it. For example, you have a path, which tells the shell where it can look for files that you reference.

When a program runs through the web server as a CGI program, it does not have that path. Any programs that you invoke in your CGI program (like 'sendmail', for example) will need to be specified by a full path, so that the shell can find them when it attempts to execute your CGI program.

A common manifestation of this is the path to the script interpreter (often perl) indicated in the first line of your CGI program, which will look something like: #!/usr/bin/perl