I am using a tied variable in perl. I often use substr to get a part of the string from the variable rather than the whole thing, but it seems that Perl uses FETCH in such a case to fetch the entire variable, then apply the substr operation and STORE the entire variable back. This seems rather innefficient to me in the case of large string as such is stored in a database. A Database migh contain megabytes of data in a container and may provide a more efficient manner of doing substring stores and fetches rather than fetching and storing the entire thing. I believe it would be a good idea to allow a STORESUBSTR and FETCHSUBSTR callback to be defined in the tied module in scalar, array, and hash tie modules. If STORESUBSTR and FETCHSUBSTR are not defined then Perl simply could revert back to the FETCH and STORE for getting the string, applying the substring change or extracting a substr, and storing back, as Perl does currently, assuring backwards compatability.

STORESUBSTR stores a string into a substring. If offset is 0, then none of the text in the original string is replaced, but rather the string is inserted into the string at $length. If offset is specified, than the bytes from $length to $length+$offset are replaced by $replacestr.

For arrays and hashs, the it would be called as this: STORESUBSTR ($obj, $key, $length, $offset, $replacestr);, and with scalars this: STORESUBSTR ($obj, $length, $offset, $replacestr);. It returns the substring replaced: return ($orig_value);.

FETCHSUBSTR requests a substr from a string.

For arrays and hashs, the it would be called as this: FETCHSUBSTR ($obj, $key, $length, $offset);, and with scalars this: FETCHSUBSTR ($obj, $length, $offset);. It returns the substring value: return ($value);.
  • Comment on Tie() and substr(), proposal for SUBSTR tie object method

Replies are listed 'Best First'.
Re: Tie() and substr(), proposal for SUBSTR tie object method
by dave_the_m (Monsignor) on Jul 02, 2005 at 15:44 UTC
    There are several reasons why this proposal wouldn't work, or would be a bad idea.

    First, optional tie methods have the habit of breaking inheritance. Consider a hypothetical popular CPAN module that uses a tied SCALAR. Suppose my code creates a subclass of this, where I override FETCH, with something like

    sub FETCH { my $self = shift; do_domething; $self->SUPER::FETCH(@_); }
    Now suppose that the CPAN maintainer updates his code to include a new FETCHSUBSTR method. I install this version, and suddenly my FETCH method is mysteriously no longer being called.

    Second, given the way the perl5 interpreter works, it's not practical to combine array subscripting and substr, so it wouldn't be possible to use *SUBSTR with arrays and hashes.

    Lastly, it would help if you explained how the various uses of the substr function were mapped to *FETCH calls, especially when substr is used as an lvalue, eg

    $ perl586 -wle 'sub f { $_[0] = "x" } $a = "abcd"; f(substr($a,0,2)); +print $a' xcd

    Dave.

      Sorry for my delay in replying. Yes, since it seems like it would create some problems, perhaps it isnt such a great idea. As far as mapping the *FETCH calls, it probably would have involved FETCHSUBSTR being called for substr($str, $len, $offset), and STORESUBSTR being called for substr($str, $len, $offset, $replacement);. Thank you for everyones replies.
Re: Tie() and substr(), proposal for SUBSTR tie object method
by ambrus (Abbot) on Jul 02, 2005 at 16:37 UTC

    I'd also like to see a FETCHPRINT method that's called when someone prints a tied variable, a FETCHMATCH method for when someone wants to match a regexp against the tied variable, a FETCHKILL variable if someone wants to kill a process whose process id is stored in a tied variable, a FETCHSIGNAL for when someone wants to use a tied variable as the signal number to the kill function, and some magical setting to solve the conflict if both the signal number and the process id are tied variables...

      Did you even read the OPs reasons for suggesting special handling for substr? If you had, you would have suggested only FETCHVEC.

        You are right.

        But seriously, I think this isn't the task of tie, rather, it's that of operator overloading. Besides the stringification operator, there could be an overloadable substr operator, which would return a part of the string. (Similarly there could be a print operator too, which would print the object. The match and kill thingy is stupid.) Then, the tied variable (which could be scalar, or array etc) could return an overloaded object. This, however, is probably still not a very good idea because, as dave_the_m explained, it would cause incomaptibilities.

Re: Tie() and substr(), proposal for SUBSTR tie object method
by holli (Abbot) on Jul 03, 2005 at 23:15 UTC
    You can always add a method to the class you tie and call it via the tied-function.
    my $substring = tied($object)->mySubstring($start, $length);


    holli, /regexed monk/