#!/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.


In reply to Re: [substr] anomaly or mine? by PodMaster
in thread [substr] anomaly or mine? by BrowserUk

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, details, 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, summary, 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.