Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Setting end position for the regexp engine

by BrowserUk (Pope)
on Sep 18, 2013 at 21:19 UTC ( #1054739=note: print w/ replies, xml ) Need Help??


in reply to Setting end position for the regexp engine

Let function substr return a lvalue which could be store in a (new type of?) scalar. The lvalue can then be used together with =~.

That is already possible.

C:\test>p1 $s = 'This is a string (with a bit in the middle) before the string en +ds';; $r = \substr $s, 18, 24;; print $$r;; with a bit in the middle $$r =~ s[([a-z]+)][\U$1]g;; print $s;; This is a string (WITH A BIT IN THE MIDDLE) before the string ends

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Setting end position for the regexp engine
Download Code
Re^2: Setting end position for the regexp engine using LVALUE
by bojinlund (Curate) on Sep 19, 2013 at 07:37 UTC
    BrowserUK thank you very much for the answer!

    I have made two new versions of the example parser using the LVALUE returned by substr.

    In version 2 I use [ $type, $first, $length ] to represent a part in @part.

    In version 3 I use [ $type, $sub_txt_ref ], where $sub_txt_ref is a LVALUE returned by substr.

    The scalar type LVALUE is new form me. Where can I read about it?

    Example of parser version 2

    Example of parser version 3

    Is updated! Removed the parameter $txt_ref.

      The scalar type LVALUE is new form me. Where can I read about it?

      Um. Beyond perldoc:substr, I'm not sure.

      It has been a part of my lexicon for so long, I've forgotten how or when I first learnt of it.

      And of course there is this, where the hereditary peer hereabouts ... (draw your own conclusions).


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      ref returns LVALUE for a reference to a PVLV, a scalar subtype. You can read about it a bit here. It's basically a scalar that's going to have magic associated with it, and it has a few extra fields for storage the magic can use.


      Magic, among other things, allows one to attach a getter and fetcher to a variable.

      You might think that

      substr($_, 3, 2) = 'foo';

      gets converted to

      substr($_, 3, 2, 'foo');

      by the compiler, but it isn't. substr is truly called as is. This allows the following to work

      my $ref = \substr($_, 3, 2); $$ref = 'foo';

      That means substr must return a magical scalar.


      There are more than one type of scalar. Some can hold an integer, some can hold a string, some can hold both. Perl automatically upgrades a scalar when necessary.

      Magical scalars require extra fields to store information about what magic is attached to the scalar. The most basic scalar subtype capable of being magical is the PVMG, but there is also PVLV. The PVLV is a PVMG with four extra fields: TYPE, TARGOFF, TARGLEN and TARG.

      A instance of substr in an lvalue context returns a PVLV. It uses TARG, TARGOFF and TARGLEN to store the three arguments passed to substr[1].

      >perl -MDevel::Peek -e"$_ = 'abcdef'; my $ref = \substr($_, 3, 2); Dum +p($$ref);" SV = PVLV(0x4d3d24) at 0x2cb29c <--- PVLV REFCNT = 1 FLAGS = (TEMP,GMG,SMG) <--- "get" magic and "set" magic IV = 0 NV = 0 PV = 0 MAGIC = 0x49f09c MG_VIRTUAL = &PL_vtbl_substr <--- Function pointers for magic MG_TYPE = PERL_MAGIC_substr(x) <--- x = substr magic TYPE = x <--- x = substr magic TARGOFF = 3 <--- For "x", start offset of substring TARGLEN = 2 <--- For "x", length of substring TARG = 0x4a8414 <--- For "x", addr of associated scalar ($_) FLAGS = 0 SV = PV(0x2c8a6c) at 0x4a8414 <--- Dump of associated scalar ($_) REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x2cab94 "abcdef"\0 CUR = 6 LEN = 12

      When you try to fetch from $$ref, the associated get magic first does something like $$ref = substr($$TARG, $TARGOFF, $TARGLEN);.

      After you assign to $$ref, the associated "set" magic effectively does substr($$TARG, $TARGOFF, $TARGLEN, $$ref);.


      Notes

      1. Missing and negative arguments are resolved before being assigned.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-07-31 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls