Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
=pod =head1 NAME Tie::Scalar::Substring - Tie strings to subranges of another string. =head1 SYNOPSIS my $s = "abcdefghijklmnop"; tie my $x, 'Tie::Scalar::Substring', \$s, 1, 5; tie my $y, 'Tie::Scalar::Substring', \$s, 2; =head1 DESCRIPTION If you need to define one or more strings in terms of being substrings of some other "fundamental" string, this module gives you exactly that. Strings so tied can be assigned; the effect is the same as assigning to lvalue C<substr>. Note that you can define a sub-string to have a certain specified length, or you can leave that parameter undefined, in which case the substring is "everything up to the end of the string", just as with C<substr>. =head1 CAVEATS The caveats are all the same as when assigning to lvalue C<substr>. You generally won't get useful results if a sub-string is defined as starting past the end of the underlying string. Assigning to one sub-string will (in general) affect the value of all other sub-strings, "magically". You may be surprised by the effect of assigning a value whose length is different from the defined length of the target string. For example: my $s = "abcdefghijklmnop"; tie my $x, 'Tie::Scalar::Substring', \$s, 1, 1; tie my $y, 'Tie::Scalar::Substring', \$s, 5, 1; print "before: '$x' '$y'\n"; $x = 'qu'; print "after: '$x' '$y'\n"; produces the following output: before: 'b' 'f' after: 'q' 'e' =head1 HISTORY 2005-07-16 jdporter Initial version. =cut use strict; { package Tie::Scalar::Substring; sub TIESCALAR { my( $pkg, $fund_str_sr, $ofs, $len ) = @_; # did you know that you can bless a reference # of type substr? defined $len ? ( bless \substr( $$fund_str_sr, $ofs, $len ), $pkg ) : ( bless \substr( $$fund_str_sr, $ofs ), $pkg ) } sub FETCH { my $self = shift; $$self } sub STORE { my $self = shift; $$self = shift; } sub LENGTH { my $self = shift; length( $$self ) } } # this block will be executed if you call this file as a script, # but not when it's require'd. It's essentially a unit test. unless ( caller(0) ) { my $s = "abcdefghijklmnop"; my $x; my $y; tie $x, 'Tie::Scalar::Substring', \$s, 5; #, 5; tie $y, 'Tie::Scalar::Substring', \$s, 7; #, 5; print "before:\n"; print "s=$s\n"; print "x=$x\n"; print "y=$y\n"; print "len(x)=", length($x), "\n"; $x = "quvwxyz"; print "\nafter:\n"; print "s=$s\n"; print "x=$x\n"; print "y=$y\n"; print "len(x)=", length($x), "\n"; } 1;

In reply to Tie::Scalar::Substring by jdporter

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (8)
    As of 2018-04-19 16:18 GMT
    Find Nodes?
      Voting Booth?