Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Seems to be a Win32 thing

As I said earlier, I get this same behaviour under HP/UX. Unix isn't windows, so it's not a "Win32" thing. :-)

even though I cannot see how to provide any sort of indication of where the problem might occur?

Well, you seem to be ignoring my conjectures about substr() being treated like an lvalue subroutine in this context.

Maybe I should be clearer about what I suspect is happening.

First of all, from perldoc -f substr: When used as an lvalue, specifying a substring that is entirely outside the string is a fatal error.

substr() is used an lvalue (left value) when it's on the *left* hand side of an equal sign. This isn't usual for function calls, but substr() is an exception.

For example, this code demonstrates a substr() used as an lvalue, where the substring lies entirely outside the string. It generates a fatal error, just like the documentation says it will.

use strict; use warnings; my $x=""; substr($x,2,1)="X"; # a fatal errror print "Alive!\n"; # not reached __END__ substr outside of string at - line 4.

Notice how this fatal error behaviour looks exactly the same as the fatal errors we saw earlier in the thread, back when we passed "substr($x,2,1)" to a function that does nothing. Hmm... suspicious!

Second of all, note that function calls can sometimes modify their callers:

use strict; use warnings; my $x=""; bar($x); print "$x\n"; #prints 10 sub bar { $_[0]=10; } # does $x=10, essentially

So, let's combine these two ideas.

Now, suppose I were to pass substr($x,2,1) to bar() instead of $x. That would be roughly equivalent to running:

substr($x,2,1)=10
, which treats our substr() call as an lvalue. (It's on the *left* side of an assignment). This is a fatal error, like we saw in point #1 above.

My question to the experts was (and maybe I didn't spell this out clearly enough):
Is the fact that a function *may* need to treat substr() as an lvalue causing it to be *always* treated that way?

My guess is "yes" (it would explain the behaviour that we both see), and my follow-up question was whether this was a design decision, or a bug.

Now do you see what I was really asking?
--
Ytrew


In reply to Re^6: substr oddity by Anonymous Monk
in thread Perl oddities by brian_d_foy

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2020-11-28 20:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?