What is the difference between for & foreach, exec &
system?

Answers were Sorted based on User's Feedback



What is the difference between for & foreach, exec & system? ..

Answer / mukesh

for and foreach are alias of each other.But syntax is different.

e.g for(my $i = 0; $i < 3; ++$i){print $i}
e.g foreach my $i (0 .. 2){print $i}

i.e in foreach we can take always only array of elements or
range of elements.But in for we dont have to take the array
or range always.here we can provide condition of the loop
where it is not true for foreach.

Is This Answer Correct ?    28 Yes 7 No

What is the difference between for & foreach, exec & system? ..

Answer / satish kumar golukonda

for and foreach works alike.
But for uses condition expression, where as foreach works on
ranges, contineous indexing.

Is This Answer Correct ?    10 Yes 3 No

What is the difference between for & foreach, exec & system? ..

Answer / maheswar reddy

The For and Foreach loop works quite same......but
Foreach loop is best way to access dynamic arrays.If we don't know the size of the array then we can't mention the range for the "FOR LOOP" in this case for loop is best

Is This Answer Correct ?    5 Yes 2 No

What is the difference between for & foreach, exec & system? ..

Answer / ramesh

Both Perl's exec() function and system() function execute a
system shell command. The big difference is that system()
creates a fork process and waits to see if the command
succeeds or fails - returning a value. exec() does not
return anything, it simply executes the command. Neither of
these commands should be used to capture the output of a
system call. If your goal is to capture output, you should
use the

$result = system(PROGRAM);
exec(PROGRAM);

Is This Answer Correct ?    12 Yes 11 No

What is the difference between for & foreach, exec & system? ..

Answer / raj

Ofcourse the syntax is completely different but the only
difference is
foreach simply gives an easy way to iterate over arrays.
foreach works only on arrays,
and will issue an error when you try to use it on a variable
with a different data type or an uninitialized variable
where as for loop works on the count length or what ever
please look in to php.net site for reference.

Thanks,
Raj

Is This Answer Correct ?    3 Yes 2 No

What is the difference between for & foreach, exec & system? ..

Answer / raj

And i have read some where i could not remember the source
but foreach is faster than the for loop.
Thanks,
Raj

Is This Answer Correct ?    1 Yes 1 No

What is the difference between for & foreach, exec & system? ..

Answer / ram nivas verma

What is the difference between the 'for' & 'foreach' loops? I know that they can be used interchangeably then what is the purpose of keeping them separate? Can anyone suggest me a good url which can tell the difference between two?

Is This Answer Correct ?    2 Yes 2 No

What is the difference between for & foreach, exec & system? ..

Answer / mvamsi

Hi Ramesh, You mean to say that we can't capture output of
the program by using exec function. But its working in my PC.
my $ls = exec('ls');

Is This Answer Correct ?    2 Yes 7 No

What is the difference between for & foreach, exec & system? ..

Answer / mahendra ratnakar

Technically, there's no difference between for and foreach
other than some style issues.

One is an alias of another. You can do things like this

foreach (my $i = 0; $i < 3; ++$i)

{ # normally this is foreach print $i, "n";}


for my $i (0 .. 2)

{ # normally this is for print $i, "n";}


- 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.

Is This Answer Correct ?    12 Yes 18 No

Post New Answer

More CGI Perl Interview Questions

How to dereference a reference?

0 Answers  


Explain grooving and shortening of arrays?

0 Answers  


Where the command line arguments are stored and if you want to read command-line arguments with Perl, how would you do that?

0 Answers  


I have one question regarding to eval function. I know eval function is use for error checking but I am not able to understand below line. eval \'exec perl -S $0 ${1+\"$@\"}\' if 0; $0 for script name $@ set if error occur

0 Answers  


Write a program that shows the distinction between child and parent process?

0 Answers  


Explain the difference between use and require?

5 Answers   Aspire,


Explain '->' in perl?

0 Answers  


what are prefix dereferencer and list them out?

0 Answers  


What is the difference between single (') and double (") quote in a string in perl?

0 Answers  


What does file test operators do in perl?

0 Answers  


what is Polymorphism in Perl?

0 Answers  


Which guidelines by Perl modules must be followed?

0 Answers  


Categories