Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

nooby question about @+, @-

by rsFalse (Hermit)
on Oct 27, 2014 at 16:46 UTC ( #1105179=perlquestion: print w/replies, xml ) Need Help??

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was expecting same answer ("6|7\n") from all code lines below. Why they are different?
while(<>){ @_=/./g; print "@-|@+\n"; ()=/./g; print "@-|@+\n"; /./g; print "@-|@+\n"; (/./g); print "@-|@+\n"; ()==/./g; print "@-|@+\n"; $_[/./g]; print "@-|@+\n"; /./g; print "@LAST_MATCH_START|@LAST_MATCH_END\n"; }
# INPUT: 1234567 # OUTPUT: 6|7 6|7 0|1 1|2 2|3 3|4 |

Replies are listed 'Best First'.
Re: nooby question about @+, @-
by toolic (Bishop) on Oct 27, 2014 at 16:54 UTC

    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.

      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'

      [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: perlquestion [id://1105179]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2021-10-22 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (85 votes). Check out past polls.

    Notices?