Yes, Perl "bare return" does things that might not be expected. Basically, I figure it is a bad idea to use a "bare" "return;" and I just don't do it. The below code is a simple demo... "return" will return something unless you tell it to return "nothing". As I mentioned before, "returning nothing" is useful in map{}, but for a normal function, I would return "undef".
Update: This part was wrong...goof in my testing! got a bit confused while doing this quickly...Oooops...I'll have to look at some other code I did awhile ago to find a better example...I think I have one somewhere. Anyway the idea of returning $x or () from within map{} is right idea to "skip" a value in output list.#!/usr/bin/perl -w use strict; sub x { my $a =5; } print x; #prints 5
sub x1 { my $a =5; return; } print x1; #still prints 5! Whoa! #"return'" returns value of last true statement. #if you don't specify something else for it to return.
Update: I don't see any basic disagreement or argument, but perhaps a misunderstanding of the application for this:sub x2 { my $a =5; return (); } print "x2:",x2; #prints "x2:" , ie nothing from sub x2
But my questions in Re^2: reset particular variable were in the context of a discussion of differences between the $var = (); and $var = undef; et al statements, differences I still don't see.
If I can "skip a value", instead of returning "undef" from the map, then say @outputlist could have fewer items in the list than @inputlist. Later when @outputlist is processed, there doesn't have to be code that says "check for undefined value", because there won't be any.@outputlist = map{..code...}@inputlist;
In reply to Re^5: reset particular variable
by Marshall
in thread reset particular variable
by abubacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |