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

Re: Re: Passing match regexs as arguments to subroutines

by ton (Friar)
on Jul 24, 2001 at 06:57 UTC ( [id://99222]=note: print w/replies, xml ) Need Help??


in reply to Re: Passing match regexs as arguments to subroutines
in thread Passing match regexs as arguments to subroutines

Oddly enough, it still won't work if you remove the parentheses; you have to force scalar on it. Guess regexs are in list context by default. Learn something new every day...

Thanks!

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

Replies are listed 'Best First'.
(MeowChow) Re3: Passing match regexs as arguments to subroutines
by MeowChow (Vicar) on Jul 24, 2001 at 07:56 UTC
    You seem to be subtly misinterpreting the way context works in perl. There is no such thing as "default" context for a given operation or expression. An expression is either in list, scalar, or void context depending upon how it is being used.

    Furthermore, the placement of parens around an expression doesn't put that expression into list context. The only time parens determine context is when they are placed around the left side of an assignment, thereby providing list context for the right side of the assignment.

    Arguments to unprototyped user subroutines are always evaluated in list context, which is what you observed in your example. However, had you prototyped your subroutine as sub _Printer ($$$$$$) { ... }, you would have seen your regex evaluated in a scalar context, regardless of whether or not there were parens around it.

    Basically, no function/operator has any say as to what context its result is provided in. The causality works in the other direction. When you observed that you had to force scalar on your regex, it was not because the regex had been in list context by default, but because you were using the regex in list context.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found