Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Assign & substitute in one statement?

by Zaxo (Archbishop)
on Jun 13, 2004 at 22:09 UTC ( #366364=note: print w/ replies, xml ) Need Help??


in reply to Assign & substitute in one statement?

Sure, just write parens in their precedence-adjusting role: ($dirfilename = $url) =~ s|\Qhttp://foo.bar.com/\E||;
Your substitution is corrected here for the presence of re metacharacters.

After Compline,
Zaxo


Comment on Re: Assign & substitute in one statement?
Download Code
Re^2: Assign & substitute in one statement?
by Aragorn (Curate) on Jun 14, 2004 at 11:23 UTC
    Sure, just write parens in their precedence-adjusting role
    ...and their list context inducing role.

    Arjen

    Update: edan is right. I was thinking about the m// operator which does return the matched subexpressions in list context.

      I had to think about it for a minute, and reread parts of the perlop manpage, but Zaxo is right, and you're wrong. Sorry.

      The s/// operator always returns the number of substitutions made, regardless of context. The parentheses here are indeed adjusting the precedence, because normally the =~ operator binds more tightly than assignment operators, = in this case. That's why without parens you first do the substitution, then assign the result to the variable. Putting the parens around the assignment forces that to happen first. In comes the handy lvalue property of assignment, so modifying the assignment with =~ has the effect of modifying the left-hand argument of the assignment. In the words of perlop:

      Modifying an assignment is equivalent to doing the assignment and then modifying the variable that was assigned to.

      --
      edan

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-09-16 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (53 votes), past polls