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

Create a function that is only available inside the scope where it is defined ?

0 Answers  


You want to read command-line arguements with perl. How would you do that?

0 Answers  


Which guidelines by Perl modules must be followed?

0 Answers  


Define dynamic scoping.

0 Answers  


Which operator in perl is used for the concatenation of two strings?

0 Answers  






How will you access an element of a perl array?

0 Answers  


Explain chomp, chop, cpan, tk.

0 Answers  


Is there any way to add two arrays together?

0 Answers  


How to implement a stack in Perl?

0 Answers  


Why we use CGI?

0 Answers   Wipro,


Differentiate use and require?

0 Answers  


How to check the status of airplane mode (enable/disable) in perl for Android mobile?

0 Answers  


Categories