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 deleting an existing file in perl programming?

0 Answers  


Give an example of using the -n and -p option.

0 Answers  


Explain arrays in perl.

0 Answers  


Can we load binary extension dynamically?

0 Answers  


What is the use of -n and -p options?

0 Answers  






How to start perl in interactive mode?

0 Answers  


what is perl language?

0 Answers  


What does perl do in linux?

0 Answers  


“The methods defined in the parent class will always override the methods defined in the base class”. What does this statement means?

0 Answers  


Where do we require ‘chomp’ and what does it mean?

0 Answers  


How to print escaping characters inside a string in perl?

0 Answers  


How to open and read data files with Perl

0 Answers  


Categories