1. On wantarray/want and context
(a) what I understand by the definition of wantarray
The wantarray function can be used inside of a subroutine definition to determine the context in which that subroutine is called. By default, such a context is available to the return statement of the subroutine. But if the context is to be known in other parts of the definition of the subroutine then wantarray needs to be used. Is this observation true ? Is the real usage of wantarray just limited to using inside of a subroutine ?
(b) In the program,
output:sub test_wantarray{ wantarray ? Y : N; } print "Hello" . &test_wa . "World\n"; print "Hello", &test_wa, "World\n";
In the first print function, the concatenation operator supplies the context to subroutine test_wantarray. Hence wantarray from inside of test_wantarray finds this context to be a scalar context and returns N. In the second print function, since the comma operator (,) doesn't supply a context of it's own, the subroutine test_wantarray is operating under the context supplied by print function. Hence wantarray from inside of test_wantarray finds this context to be list context and hence returns Y. Is this what davido meant by "IF YOU COULD call wantarray on print function it would return true." and am I successful in calling wantarray on print function (i.e. in the case of second print function)?HelloNWorld HelloYWorld
(c) Since the above subroutine test_wantarray is just one line, if the subroutine call is replaced in the above print function to be,
it prints: HelloNWorld. Is it that wantarray can only be used inside a subroutine? Can you explain about what you mentioned earlier? i.e. "you are not calling wantarray in the context that print supplies, in the context that ternary operator supplies" ?print "Hello", (wantarray ? Y : N), "World\n";
Why this scalar context of ternary operator doesn't come into play inside of the subroutine test_wantarray ?sub test_wantarray{ wantarray ? @array: \@array; }
(d) Refering to the print function in the above sub section (b)
returns true confirming that the print function is providing a list context to it's arguements. If such is the case,print "Hello", &test_wa, "World\n";
(e) I looked at want module documentation (http://dev.perl.org/perl6/rfc/21.html) but wasn't able to understand. Is the real use of this,
(i). want should be able to distinguish between various type of scalar contexts or various type of list contexts when used inside of a subroutine . Can you please provide an example ?
(ii). how does it get down to small constructs (as opposed to wantarray) ? Can you please provide an example ?
2. If the return value of an expression is, undef or "" or number 0 would they all evaluate to false value in boolean context?
3. Can we confirm from the below program that, the internal value of undef is the ASCII null value (ie. ASCII code: 0)
if (undef eq "") { print "undef evaluates to empty string in string context\n"; } if (undef == 0) { print "undef evaluates to zero in numeric context\n"; }
4. In the below code
Did you get any chance to find out what exaclty causes $count to return the number of times abc appears in $s instead of abcwhen you mentioned that "....I'm not sure what magic causes $count to come out as a number instead of "abc". It may be that what I'm missing has to do with the difference between m/foo/g and s/foo/bar/g. The latter I use much more frequently than the former, but they work differently. I'll try to remember to look into that and reply again if I figure out exactly what's going on there."$count = () = $s =~ /abc/g;
5. Just wanted to know, where did you come accross the information that your earlier mentioned i.e. "....way back in the day it was decided that the array interpolation inside double quoted string should have the same syntax as that of sprintf of C. So when array interpolation was added, hash interpolation along the same lines couldn't be. This will be remedied in PERL 6....". I just needed this detail just to understand why hash interpolation couldn't be added.
In reply to Re: Re: string context and list operators (was Re: Array in scalar context.)
by Anonymous Monk
in thread Array in scalar context.
by the_0ne
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |