Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: substr oddity

by Anonymous Monk
on Mar 04, 2005 at 19:05 UTC ( [id://436706]=note: print w/replies, xml ) Need Help??


in reply to Re^3: substr oddity
in thread Perl oddities

Okay, I'm confused... is that your entire output? Was the word "Alive" in there somewhere?

It should print "Alive!" if the code doesn't crash. I don't see that in your output. If that's your entire output, then you get what I do: a crash on the substr() call. Similarly, you'll get the same kind of crash if you assign to substr(): that is, treat it like an Lvalue.

My guess is that because @_ can be assigned to, substr() behaves like an Lvalue when passed as a subroutine argument. If that's true, it's odd enough to be an oddity. :-)
--
Ytrew

Replies are listed 'Best First'.
Re^5: substr oddity
by BrowserUk (Patriarch) on Mar 04, 2005 at 19:28 UTC

    Yep! That's the entire output. I've added a couple of print $]; statements, one in an END{} block, to these to show that I get some output, which means that STDOUT must be being flushed, which I assume is a clean-up time operation:

    P:\test>perl -l END{ print $] } print $]; use strict; use warnings; my $x=""; foo( substr($x,2,1) ); # crashes here print "Alive!\n"; # not reached sub foo {} ^Z 5.008004 substr outside of string at - line 6. 5.008004 P:\test>c:\perl561\bin\perl5.6.1.exe -l END{ print $] } print $]; use strict; use warnings; my $x=""; foo( substr($x,2,1) ); # crashes here print "Alive!\n"; # not reached sub foo {} ^Z 5.006001 substr outside of string at - line 6. 5.006001 P:\test>

    However, if I remove the subcall from the picture:

    P:\test>perl use strict; use warnings; my $x=""; print substr($x,2,1); # crashes here print "Alive!\n"; # not reached ^Z substr outside of string at - line 4. Use of uninitialized value in print at - line 4. Alive! P:\test>c:\perl561\bin\perl5.6.1.exe use strict; use warnings; my $x=""; print substr($x,2,1); # crashes here print "Alive!\n"; # not reached ^Z substr outside of string at - line 4. Use of uninitialized value in print at - line 4. Alive! P:\test>

    Which indicates that the execution path is being truncated or short ciruited when the subcall is in place, but no crash, just a silent move to the END{} of the program--which isn't very freindly.

    Seems to be a Win32 thing, and one probably worth reporting even though I cannot see how to provide any sort of indication of where the problem might occur?


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      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

        Right! Before we get into a harranging match, I don't think that you studied the program output from my last post properly. If you did, then we are simply agreeing, but do not know it yet :).

        The point I was making, was that on Win32, I never get any indication that your snippet crashes on my machine. Nothing. Zip. Nada. Not a jot:)

      • Nothing in the system or application event logs that always log if an program segfaults or otherwise terminates abnormally.
      • Nothing on the terminal that indicates anything is at fault--except the complete absence of your "Alive!" message. (which is something, but only in the "it didn't happen" way, and could not be classed as "a crash!".
      • No Dr. Watson, or system pop-up telling me that perl crashed in some way.

        So, by your description of "crash", I assumed you were getting some indication on your system that Perl had crashed--such as the "core dumped" message.

        If you are not getting this message, then your characterisation of this as a "crash" maybe accurate, in as much as Perl is certainly taking an abnormal path to completetion, but I usually associate the term "crash" with something the OS becomes aware of and reports, rather than (as now seems a possible interpretation of your use of that term) an internal to perl, "bottle out of here cos we're confused and don't bother telling the user anything" behaviour that I am seeing.

        To put that more clearly, calling substr in a lvalue context, with parameters that lie entirely outside the target string, causes a fatal-but-silent termination of the Perl process.

        If this is the behaviour that you are seeing under HP/UX, then I agree it is most definitely worthy of the term "Perl oddity", and I apologies for my having misunderstood you.

        It does not however, cause what I would term a "crash", at least on my system. :)

        So, are we agreeing; disagreeing; agreeing to disagree or disagreeing to agree?

        I'd like to achieve the former but I would settle for the second to last, but I think I have acquired a reputation for tending to the latter--though, of course, I disagree with that assessment :)


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found