Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: substr oddity

by BrowserUk (Patriarch)
on Mar 04, 2005 at 06:05 UTC ( [id://436463]=note: print w/replies, xml ) Need Help??


in reply to substr oddity
in thread Perl oddities

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

P:\test>perl -wle"sub foo{}; my $s =''; foo( substr $s, 2, 1 );" substr outside of string at -e line 1.

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

Replies are listed 'Best First'.
Re^2: substr oddity
by Anonymous Monk on Mar 04, 2005 at 18:14 UTC
    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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found