Hello, Perl Monks!

I am putting together a Lightening Talk for Toronto.pm for tomorrow night on Perl's lvalue subroutines feature. For its "grand finale" I was hoping I could (more or less) recreate the lvalue version of substr, i.e.

my $graffito = 'Richard was here.'; substr($graffito, 8, 3) = 'is'; # now, it's "Richard is here."

As you can see in the following program and its output, I am "almost" there with my mysubstr lvalue subroutine. The part I've not figured out how to do is to somehow get the subroutine aware of what the rvalue in the operation is. Please read my comments in the program to see where my line of thinking is going.

rdice@tanru:~$ cat lvalue2.pl && ./lvalue2.pl #!/usr/bin/perl -w ###################################################################### # # lvalue2.pl # Another quick, dirty and simple program meant to # illustrate the creation and use of lvalue subroutines. # This is part of my "What Fresh L is this?" Lightening # Talk for the Toronto Perl Mongers, Thu 26 Sept 2002. # # Author: Richard Dice (rdice@pobox.com) # Date: Tue 24 Sept 2002 # ###################################################################### use strict; # Always use strict. ALWAYS USE STRICT. my $graffito; $graffito = 'Richard was here'; print ((substr($graffito, 8, 3) = 'is'), "\n"); # to see return valu +e print "$graffito\n"; # to see end result print "\n"; # whitespace between + outputs $graffito = 'Richard was here'; print ((mysubstr($graffito, 8, 3) = 'is'), "\n"); # to see return valu +e print "$graffito\n"; # to see end result # Were 'mysubstr' to work correctly, then the two outputs should be # identical exit 0; sub mysubstr : lvalue { my @chars = split //, $_[0]; my $pos = $_[1]; my $width = $_[2]; # Wouldn't it be nice if the rvalue was added onto the end of @_? # As it stands, I have no idea how to get at the rvalue. I'll # just pretend that this is how it should be handled for the sake # of this example. my $rvalue = $_[3]; # For all you pedants out there, I know that my algorithm for # putting the string back together doesn't exactly equal what # the real 'substr' function would at its edge cases. # That's not the point of this example. $_[0] = (join '', @chars[0..$pos-1]) . $rvalue . (join '',@chars[$pos + $width .. $#chars]); $rvalue; # This seems like the easiest way to return what the # real 'substr' returns without mucking anything up. } is Richard is here Use of uninitialized value in concatenation (.) or string at ./lvalue2 +.pl line 54. is Richard here rdice@tanru:~$

Any ideas, folks? Btw, I'm running 5.8.0, but this should work (if indeed it did work) back to 5.5.3 at least.

Cheers,
Richard


In reply to Recreation of lvalue substr function using lvalue subroutines by Dice

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.