Thank you for your detailed responses. This relly helps in understanding the concepts. It took me sometime to go over your answers. Based on these here are the questions I have. Please let me know your answers. Thanks much

1. Is a string context same as scalar context where the scalar value is "stringified" ?

2. Is a numeric context same as scalar context where the scalar value is turned to numeric so as to perform any numeric operation ?

3. Is a boolean context same as scalar context where the scalar value is booleanized ?

4. In code

$! = 10; print "Hello", $!>0, "World\n";
prints Hello1World But,
$! = 0; print "Hello", $!>0, "World\n";
prints HelloWorld instead of Hello0World. So the print function stringifies boolean true value of the expression $!>0 to "1" but why not the boolean false value 0 to "0" ?

5. When a string is used in numeric context (say, in an expression such as: "h">=0 ) it returns 0 as it's value though a warning (such as: "Argument "h" isn't numeric in numeric gt (>)" ) is thrown. Speaking conversly, is there any case where a numeric value in string context returns empty string ?

6. davido (in his earlier reply to this thread) mentions that "If you could call wantarray on print it would return true. And so, @foo is evaluated in list context when evaluated as argument of 'print'"

But if I try ,

print wantarray ? YES : NO , "\n";
it prints NO indicating that print doesn't want an array. So what did davido mean in his comment then?

7. When you said in your first reply that, "join function is a list operator, like print" are you refering to the syntactial behavior of print (because it can take an array as one of it's arguments) instead of the symantics of it ? Similarly when you say "print function flattens the list and processes each of the elements in string context, are you refering to it's symantics ?

8. The 'Learning Perl' book - 3rd Edition on page: 125 while describing "split" function mentions that split is an operator instead of a function. Can you please throw some light on this ?

9. You said that "Way back in the day, it was decided that Perl should have the same sprint syntax as C, and so when array interpolation was added , hash interpolation along the same lines couldn't be". Can you explain this little bit. I also tried this in C

int main() { char buf[50]; char a[] = {'a', 'b', 'c'}; sprintf (buf, "Hello %s here\n", a); printf ("%s", buf); }
this prints: Hello abHello abHello ab hereBut not Hello a b c here

10. Can a Perl programmer code functions which do string magic like $! does ? i.e. If $! in numeric context returns 2, in string context, instead of returning stringified 0, it returns the string "No such file or directory".

11. If an array in scalar context returns it's length why wasn' the same thing done for lists as well? Is this due to a similar kind of reason such as sprintf style from C was not possible for hash interpolation ?

12. $count = () = $data =~ m/and/g; The $count has the value of number of times the pattern abc appears in $data. What magic that () is doing . Doesn't the operation $count = () cause the list to be operated in scalar context such as

$count = ("abc", "abc", "abc")
which shall cause $count to have the value of last element of the list which is "abc" instead of 3. Can () be used to force list context ?


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.