Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is for(1) always the same as do in perl?

Tags:

perl

for(1){
  print 1;
}

do {
  print 1;
}

Is it true?

Or is there any special case these two doesn't equal?

like image 466
asker Avatar asked Nov 28 '22 03:11

asker


1 Answers

One difference is that for(1) sets $_ to the value of 1, as well:

for(1){
    print $_;  # prints 1
}

Also, do returns the value of the last command in the sequence:

my $x = do { 1 };  # $x = 1
my $y = for(1){ 1 }; # invalid
like image 190
Flimzy Avatar answered Dec 18 '22 22:12

Flimzy