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??

Yes, there are some subtle points that I didn't go into.

First, making @a ||= ... the same as @a = @a || ... would just be leaving a trap. So it is actually a good thing (IMHO) that @a ||= ... is fatal rather than just being identical to @a = @a || .... (And I'm pretty sure that this fatalness was more just a result of implementation details than of extra work to not leave a non-fatal trap.)

I don't recall the details, but I do recall that there are some subtle differences even between $a = $a || ... vs. $a ||= .... The differences boil down to subtle differences in how $a ||= ... is implemented, and these boil down (IMHO) mostly to matters of efficiency of implementation.

$a = $a || ... actually deals with $a twice while $a ||= ... deals with $a once.

So one way to think about why @a ||= ... is fatal is to realize that it isn't just @a = @a || ... but that both of the "@a"s in that expression are dealt with in a single go. So, rather than being like @a = 0+@a || ..., it ends up more like 0+@a = 0+@a || ....

$ perl -e '@a ||= 1..3' Can't modify array dereference in logical or assignment (||=) ... $ perl -e '0+@a = 0+@a || 1..3' Can't modify addition (+) in scalar assignment ...

Or you can think of it more like scalar @a = scalar @a || ..., like you guessed.

If so, it seems strange that perlop#Assignment-Operators doesn’t highlight this important semantic difference

But it isn't an important semantic difference. It is a subtle difference that mostly doesn't matter. The case where it easily matters is when you try to assign to an array. But that difference isn't "important" because the difference is between something that fails loudly and something that does something you didn't intend.

op= is only useful on scalars. So there are some subtle details of implementation that are quite difficult to even intentionally arrange for them to matter when using a scalar. And those details are basically due to optimizations, which makes them much more likely to change in future versions of Perl. So these are not the types of things to try to document.

The thing to document about using op= on an array would be to simply note that it doesn't work. Trying to document inaccurate explanations about subtle implementation details related to that isn't particularly useful.

- tye        


In reply to Re^7: Conditional array initialisation? (documented?) by tye
in thread Conditional array initialisation? by BrowserUk

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 goofing around in the Monastery: (3)
As of 2024-04-20 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found