Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: substr oddity

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


in reply to Re: substr oddity
in thread Perl oddities

This doesn't seem to happen on Win32 5.8.4 (AS 810). Which version are you using?

Well, I'm running version "5.8.6 built for PA-RISC2.0-thread-multi-LP64", running on HP/UX. I also get the same behaviour for 5.6.1.

But your snippet doesn't actually demonstrate the problem: you get the warning message, but can't actually detect whether the code crashed. Here's what I get when I add a trace statement after the call to substr().

$ perl -wle 'sub foo{}; my $s=""; foo( substr($s,2,1) );print "Not rea +ched\n"' substr outside of string at -e line 1. $
Note that "not reached" is never reached, and is not printed, because the code crashes on the substr() call.
--
Ytrew

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

    Fair enough, but your snippet also doesn't crash on my machine for exactly the same reasons:

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

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

        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.

Log In?
Username:
Password:

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

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

    No recent polls found