Thanks again for your reply. Since I am new to PERL it takes a long time to go through the responses and understand them. Once again here are my questions. Thanks for your help.

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,

sub test_wantarray{ wantarray ? Y : N; } print "Hello" . &test_wa . "World\n"; print "Hello", &test_wa, "World\n";
output:
HelloNWorld HelloYWorld
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)?

(c) Since the above subroutine test_wantarray is just one line, if the subroutine call is replaced in the above print function to be,

print "Hello", (wantarray ? Y : N), "World\n";
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" ?

If the ternary operator context is scalar how can it return a list if the expression for this ternary operator evaluates to true and an array is to be returned? i.e. in cases such as,
sub test_wantarray{ wantarray ? @array: \@array; }
Why this scalar context of ternary operator doesn't come into play inside of the subroutine test_wantarray ?

(d) Refering to the print function in the above sub section (b)

print "Hello", &test_wa, "World\n";
returns true confirming that the print function is providing a list context to it's arguements. If such is the case,
why provide a list context to it's arguments only to see to it that print cannot operate on them (or rather it has to flattens them)? Why not just provide a "flattened list context" where the argument list/array isn't seen but only the individual elements within the list/array ? i.e. in your earlier reply you mentioned "print and join both take a flattened list". What am I missing here ?

(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

$count = () = $s =~ /abc/g;
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."

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.