Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I think I've struck it before ... something to do with the wonderful Copy-On-Write, IIRC.

Quite the opposite. If anything, it's about the lack of copy on write.

$x and $y are references to objects. You are copying the reference, but not the referenced object (on write or otherwise).

You want $y = $x->copy(); to create a clone.

Is there some way (apart from building perl without COW) to guard against getting bitten by this ?

To avoid getting bit by this, only uses classes that provide immutable objects.

Also, you'll need to avoid references to arrays and hashes, as the same issue is found there.

my $x = [ 4 ]; my $y = $x ++$_ for @x; say @y; # 5!!

Even then, you'll have to worry about aliases.

Is it a bug in perl ?

It's a performance concession. Using immutable objects has a high cost.

You brought up Perl's COW mechanism for strings. It's specifically a mechanism to mitigate these costs. For strings. Imagine having to build COW into all your classes... And how it would work for classes that reference external (to perl) resources?


In reply to Re: Action at a distance by ikegami
in thread Action at a distance by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found