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

Re: [substr] anomaly or mine?

by PodMaster (Abbot)
on Aug 20, 2002 at 01:25 UTC ( [id://191361]=note: print w/replies, xml ) Need Help??


in reply to [substr] anomaly or mine?

#!/usr/bin/perl -w use strict; $\="\n"; ## first, to BrowserUk's misunderstanding, an explanation my $s = 'ABCD'; print substr($s,0,2)=''; # EXPECT 'CD', what's left print $s; # CD $s = 'ABCD'; print substr($s,0,2,''); # EXPECT 'AB', print $s; # CD # perldoc -f substr # An alternative to using substr() as an lvalue is to specify the # replacement string as the 4th argument. This allows you to # replace parts of the EXPR and return what was there before in # one operation, just as you can with splice(). my @B = 1..4; print splice(@B,0,2,()); # expect 12 print @B; # expect 34 # perldoc -f splice # Removes the elements designated by OFFSET and LENGTH from an # array, and replaces them with the elements of LIST, if any. In # list context, returns the elements removed from the array. # now to tye's argument, saying that substr($s,0,2)= EXPR # doesn't return EXPR and is therefore a bug # perldoc -f substr # You can use the substr() function as an lvalue, in which case # EXPR must itself be an lvalue. $s = '1234'; @B=(); $B[0] = substr($s,0,2) = 'ab'; print $s; # EXPECT ab34 print "@B"; # EXPECT ab print "ASSIGN '' "; $s = '1234'; @B=(); $B[0] = substr($s,0,2) = ''; print $s; # expect 34 print "@B"; # EXPECT 34, cause '' wouldn't be an lvalue print "ASSIGN undef "; $s = '1234'; @B=(); $B[0] = substr($s,0,2) = undef; print $s; # expect 34 print "@B"; # EXPECT 34, cause undef wouldn't be an lvalue ## CONCLUSION ## if '' and undef are lvalues, then this is a feature, and not a bug __END__ CD CD AB CD 12 34 ab34 ab ASSIGN '' 34 34 ASSIGN undef Use of uninitialized value in scalar assignment at BrowserUk.substr.pl + line 51. 34 34

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: [substr] anomaly or mine?
by BrowserUk (Patriarch) on Aug 20, 2002 at 02:46 UTC

    In the perlfunc:substr docs, I can find only two references to what it returns:

    • Extracts a substring out of EXPR and returns it. (2 and 3 arg version)

    and

    • This allows you to replace parts of the EXPR and return what was there before. (4-arg version)

    The latter isn't really relevant as I'm using the 3-arg version, but it does describe one of the two behaviours I would have expected.

    If the 3-arg version as an lvalue were consistant with the 4-arg version, I would expect the replaced part, ('quick ' in my short demo program below) to be returned.

    If it was consistant with assignment, then I would expect either the thing being assigned (''), or the result of the assignment ('the brown fox');

    What is actually being returned is 'brown '

    What is happening is that the substitution (deletion) is being performed using the offset and length, AND THEN the same values are being re-used to extract a return value FROM THE RESULTANT of the assignment.

    Whilst I can explain what is happening, using the offset and length *twice* is at least strange. Doing so in order to provide a return value that bears no relationship to either the sub-string specified by the programmer, nor the resultant of the assignment simply make no sense at all.... leastwise, not to me.

    #! perl -w my $s = qq(the quick brown fox); print substr($s, 4, 6) = '', $/; __END__ #output C:\test>191356 brown C:\test>

    With respect to if '' and undef are lvalues, then this is a feature, and not a bug

    You rightly point out that the docs say

    You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue.

    However,

    substr EXPR,OFFSET,LENGTH,REPLACEMENT

    substr EXPR,OFFSET,LENGTH

    substr EXPR,OFFSET

    The EXPR that must be a LVALUE is the first parameter to substr, not the value being assigned to it.

    I can explain what is happening. I still contend that it is neither documented that way nor intuatively expected. Nor, in my opinion, is it sensible.

    As it is, I am sufficiently convinced that this is a bug that I will send a perlbug report in along with the bulk of this post minus references about our debate. A link to this entire thread, and allow them to decide whether this is a documentation change, and code fix or simple a vagary of "BrowserUk's missunderstanding".


    What's this about a "crooked mitre"? I'm good at woodwork!
Re: Re: [substr] anomaly or mine?
by dpuu (Chaplain) on Aug 20, 2002 at 01:37 UTC
    Your first EXPECT does not fully distinguish the actual behaviour that feels anomalous:
    my $s = 'ABCDE'; print substr($s,0,2)=''; # EXPECT 'CD' or 'CDE'?
    The actual is 'CD'; intuition would expect 'CDE'

    perhaps a better test case is

    my $s = 'ABCDE'; print substr($s,0,2)='1'; # EXPECT '1C' or '1CDE'?
    again, actual is "1C". --Dave

      My intuition would expect:

      my $s = 'ABCDE'; print substr($s,0,2)='',$/; # EXPECT '', GET 'cd'? print substr($s,0,2)='foo',$/; # EXPECT 'foo', GET 'fo'?

      ..in parallel with all other assignments:

      my $s = 'ABCDE'; print $s='',$/; # EXPECT '' print $s='foo',$/; # EXPECT 'foo'

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

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

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

    No recent polls found