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

Write an expression or perl script to identify the entered ip address is valid or not?

5 Answers   HCL,


Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.

15 Answers  


Explain what is perl language?

0 Answers  


What is grep used for in perl?

0 Answers  


What is the use of 'ne' operator?

0 Answers  






How can information be put into hashes?

0 Answers  


Which feature of perl provides code reusability?

0 Answers  


Explain lexical variables.

0 Answers  


What is hash?

0 Answers  


Explain the functioning of conditional structures in perl.

0 Answers  


How do you give functions private variables that retain their values between calls?

0 Answers  


Does Perl have reference type?

0 Answers  


Categories