in reply to Re^2: A cleaner way of scoping variables
in thread A cleaner way of scoping variables
You must make sure that your do blocks always evaluate to an intended result. In this case, you have to add an else { undef } clause.
Indeed.
In your case, the value of $foo is assigned to $bar if $foo is false.
Perl is a bit wierd about the return value of a loop of which the body never runs. This code:
prints:#!/usr/bin/perl -w my $true = "foo"; my $false = "0"; my $dc = "bar"; warn do { $dc if $false }; warn do { $dc unless $true }; warn do { $dc while $false }; warn do { $dc until $true }; warn do { if ($false) { $dc } }; warn do { unless ($true) { $dc } }; warn do { while ($false) { $dc } }; warn do { until ($true) { $dc } }; warn do { $dc if "0" }; warn do { $dc unless "foo" }; warn do { $dc while "0" }; warn do { $dc until "foo" }; warn do { if ("0") { $dc } }; warn do { unless ("foo") { $dc } }; warn do { while ("0") { $dc } }; warn do { until ("foo") { $dc } }; __END__
Useless use of private variable in void context at a line 9. Useless use of private variable in void context at a line 10. Useless use of private variable in void context at a line 13. Useless use of private variable in void context at a line 14. Useless use of private variable in void context at a line 21. Useless use of private variable in void context at a line 22. 0 at a line 7. foo at a line 8. 0 at a line 9. foo at a line 10. 0 at a line 11. foo at a line 12. 0 at a line 13. foo at a line 14. 0 at a line 15. foo at a line 16. 0 at a line 17. Warning: something's wrong at a line 18. 0 at a line 19. Warning: something's wrong at a line 20. Warning: something's wrong at a line 21. Warning: something's wrong at a line 22.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: A cleaner way of scoping variables
by Aristotle (Chancellor) on Aug 09, 2004 at 14:24 UTC | |
by ikegami (Patriarch) on Oct 21, 2005 at 15:09 UTC | |
|
Re^4: A cleaner way of scoping variables
by ambrus (Abbot) on Nov 03, 2007 at 17:01 UTC |