Answer:
There's no difference between them.
e.g.
# For loop example
for ($count=1; $count<=@words; $count++){
if($words[$count-1] eq "the" ) {
print ("found the word 'the'\n");
}
}
# foreach loop example
foreach $word(@words) {
if ($word eq "the" {
print ("found the word 'the'\n"); }
}
# while loop example
$count =1;
while ($count<=@words) {
if($words[$count-1] eq "the" ) {
print ("found the word 'the'\n");
}
$count++;
}
2) What is the difference between exec and system?
Answer:
exec runs the given process, switches to its name and never returns
e.g. exec(PROGRAM);
system folks the given process, wait for its to complete and then return.
e.g. $result = system(PROGRAM);
3) What does this symbol mean '->'?
Answer:
In Perl, it is an infix dereference operator, for array or hash key, or a subroutine, then the ihs must be a reference. It can also used as method invocation.
The arrow operator also allows you to dereference references to arrays or hashes. The arrow operator makes more complex structures easier to read. The first example shows accessing an element of an array reference:
use strict;
use warnings;
my $array_ref = ['apple', 'banana', 'orange'];
print "My first fruit is: " . $array_ref->[0] . "\n";
This would produce the following output:
The next example shows accessing elements of an hash refernce:
use strict;
use warnings;
my $hash_ref = {name => 'Becky', age => 23};
foreach my $k (keys %$hash_ref) {
print "$k: " . $hash_ref->{$k} . "\n";
}
This produces the following output:
name: Becky
Subroutine references work the same way, using parenthesis:
#!/usr/bin/perl
use strict;
use warnings;
my %hash = (frogs => sub {print "Frogs\n"});
$hash{$frogs}->();
4) Perl regular exp are greedy. What is it mean by that?
Answer:
It tries to match the longest string possible.
5) What is the difference between C++ and Perl?
Answer:
1. Perl can have objects whose data cannot be accessed outside its class,
but C++ cannot do it.
2. Perl can use closures with unreachable private data as objects,
and C++ doesn't support closures.
e.g. my $print_hello = sub { print "Hello, world!"; }
$print_hello->();
3. C++ does support pointer arithmetic via 'int *ip=(int*)&object', allowing you do
look over the object. Perl doesn't support pointer arithmetic.
4. C++ supports '#define private public' to change the access right to foreign object. Perl doesn't support them.
No comments:
Post a Comment