jesuashok has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I've run into this strange behaviour when playing with trying to make functions return different values from the number of parameters they were called with. If you test @_, and that you return from a do { ... } block where a variable is localized, the return value is undef if you don't add an else statement.
#!/usr/bin/perl -w use strict; sub foo { if (@_) { return do { my $dummy; 1; }; } else { return 0; } } # let's just remove the else sub bar { if (@_) { return do { my $dummy; 1; }; } return 0; } print foo().' '.foo('baz')."\n"; # that was expected print bar().' '.bar('baz')."\n"; # undef
################################################## Expected output: 0 1 0 1 ################################################## Actual output: 0 1 Use of uninitialized value in concatenation (.) or string at ./undef.p +l line 24. 0 ##################################################
"Keep pouring your ideas"
2006-10-07 Unapproved by planetscape once evidence of habitual plagiarism uncovered.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bad return value from a block with variable localization
by BrowserUk (Patriarch) on Apr 10, 2006 at 08:59 UTC | |
Re: Bad return value from a block with variable localization
by codeacrobat (Chaplain) on Apr 10, 2006 at 11:41 UTC | |
Re: Bad return value from a block with variable localization = PLAGIARISM
by liverpole (Monsignor) on Oct 06, 2006 at 13:52 UTC | |
by jdporter (Paladin) on Oct 06, 2006 at 17:21 UTC | |
by liverpole (Monsignor) on Oct 06, 2006 at 20:06 UTC |
Back to
Seekers of Perl Wisdom