Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Why won't say act like say when its tied?

by Anonymous Monk
on Mar 07, 2011 at 15:53 UTC ( [id://891838]=note: print w/replies, xml ) Need Help??


in reply to Why won't say act like say when its tied?

As a side question, why is say implemented as { local $\="\n"; print LIST } ? The obvious implementation, to me, is print LIST, "\n". Is there a case where that results in bad behavior?

Replies are listed 'Best First'.
Re^2: Why won't say act like say when its tied?
by ELISHEVA (Prior) on Mar 07, 2011 at 17:51 UTC

    print appends the value of $\ to the end of whatever is passed to print, so if $\="***\n", then {print "hello", "\n"; } outputs "hello\n***\n" not plain "hello\n". That's why it is important for say to set local $\="\n" and not just add "\n" to the end of the list of things to print. Hope that helps.

      Thanks, it does. So the intended behavior for say is to write exactly what the programmer intended plus \n without side effects from punctuation variables. Good to know.
Re^2: Why won't say act like say when its tied?
by ikegami (Patriarch) on Mar 07, 2011 at 18:12 UTC

    It's only done that way for tied handles. Existing tie modules had to support say without being changed, and changing $\ allows that. I suppose they could have done

    local $\; ->PRINT(@_); ->PRINT("\n");

    But if $\ is being localised anyway, might as well do

    local $\ = "\n"; ->PRINT(@_);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found