in reply to Re: Scalar Vs. List context
in thread Scalar Vs. List context

What has been particularly confusing is that in the literature I have gone through-so far-there is nothing that clearly addresses this distinction and the "usage outcomes" of these contexts with supporting examples in a central manner like a chapter or topic , I thought it was not serious in the start until I have landed at the regexes and that is when I am fully aware I need to understand this before missing the chance.. and for the benefit of everyone who is caught in a similar fix I thought I am obliged to talk this out with the Monks and seek their wisdom therefore, consider this
$text="Tommy tom ticked a tick at the ticket counter"; $t_count=($text=~ y/[tT]/t/); print "This text has $t_count Ts\n"; #result #This text has 9 Ts
and this
$_ = "1.0 and 2.4 and 310 and 4.7 and so on"; @a = m/([\d|\.]+)\D+/g; print "@a\n"; #result #1.0 2.4 310 4.7
so we used the =~ in the first example and the only = in the second example (matching is used here with list context, but I would know it only after having run the code not while reading the code), this might be very plain to see for the expert but not for me. Hence I want to know the parameters for using scalar and list contexts, the differences between these contexts and the common pitfalls the novice would come across with regard to this. So what I seek is a short sharp to the point explanation of what list context usage WOULD return and what scalar context usage WOULD return for some of the operations and techniques in Perl...
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Replies are listed 'Best First'.
Re^3: Scalar Vs. List context
by jrsimmon (Hermit) on Jul 11, 2009 at 18:53 UTC
    I think what you're confused with is matching against the special variable $_, not list context vs scalar context. Note that this:
    $_ = "1.0 and 2.4 and 310 and 4.7 and so on"; @a = m/([\d|\.]+)\D+/g; print "@a\n"; #result #1.0 2.4 310 4.7
    can be rewritten as this
    $_ = "1.0 and 2.4 and 310 and 4.7 and so on"; @a = ($_ =~ m/([\d|\.]+)\D+/g); print "@a\n"; #result #1.0 2.4 310 4.7
    without changing the meaning whatsoever. Your two examples only differ in that the first matches against a defined scalar, whereas the second matches against $_.