Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: nooby question about @+, @-

by toolic (Bishop)
on Oct 27, 2014 at 16:54 UTC ( [id://1105180]=note: print w/replies, xml ) Need Help??


in reply to nooby question about @+, @-

Tip #1 from the Basic debugging checklist: warnings

Useless use of numeric eq (==) in void context at ... Useless use of array element in void context at ... 6|7 6|7 0|1 1|2 Use of uninitialized value in numeric eq (==) at ... 2|3 3|4 4|5

I believe /./g; is evaluated in scalar context, and only one match is done, not 7.

(/./g); is also evaluated in scalar context, and picks up at the position of the previous match.

Replies are listed 'Best First'.
Re^2: nooby question about @+, @-
by ikegami (Patriarch) on Oct 28, 2014 at 13:27 UTC
    1. @_=/./g;: The RHS of a list assignment operator is evaluated in list context.
    2. ()=/./g;: The RHS of a list assignment operator is evaluated in list context.
    3. /./g;: A expression statement followed by another statement is evaluated in void context[1].
    4. (/./g);: A expression statement followed by another statement is evaluated in void context[1].
    5. ()==/./g;: The RHS of a comparison operator is evaluated in scalar context. [Updated]
    6. $_[/./g];: The index expression of an array element is evaluated in scalar context.
    7. /./g;: The statement at the end of a while loop is evaluated in void context.

    The behaviour of the match operator in void context doesn't appear to be documented, but is sensibly acts as if it was evaluated in scalar context.

    $ perl -E'$_="abc"; /./g; /(.)/g; say $1' b

    1. Not scalar context, as previously mentioned.
      5.  ()==/./g;: The RHS of a list assignment operator is evaluated in list context.

      Visually tricky, this is actually a pointless comparison evaluating in void/scalar context:

      c:\@Work\Perl>perl -wMstrict -le "$_ = 'abc'; ()== /./g; /(.)/g; print qq{'$1'}; " Useless use of numeric eq (==) in void context at -e line 1. Use of uninitialized value in numeric eq (==) at -e line 1. 'b'

        Oops, fixed.
Re^2: nooby question about @+, @-
by ikegami (Patriarch) on Oct 28, 2014 at 13:34 UTC

    [dup]

    1. @_=/./g;: The RHS of a list assignment operator is evaluated in list context.
    2. ()=/./g;: The RHS of a list assignment operator is evaluated in list context.
    3. /./g;: A expression statement followed by another is evaluated in void context[1].
    4. (/./g);: A expression statement followed by another is evaluated in void context[1].
    5. ()==/./g;: The RHS of a list assignment operator is evaluated in list context.
    6. $_[/./g];: The index expression of an array element is evaluated in scalar context.
    7. /./g;: The statement at the end of a while loop is evaluated in void context.

    The behaviour of the match operator in void context doesn't appear to be documented, but is sensibly acts as if it was evaluated in scalar context.

    $ perl -E'$_="abc"; /./g; /(.)/g; say $1' b

    1. Not scalar context, as previously mentioned.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1105180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found