string variable to hold a lengthy string

Discussion in 'Perl Misc' started by John, Mar 30, 2012.

  1. John

    John Guest

    I have a rather lengthy string like this:

    $page = <<HERE;
    <input type="hidden" value="%1">
    </form>
    ....(a couple hundred html lines)
    HERE
    $page=~s/%1/$myvar/ge;
    print $page;


    Is there some limit to how long a string the $page variable can hold?
    Another question:
    The the line
    $page=~s/%1/$myvar/ge;
    processes the lengthy string. Will I run into some limitations there?
    will that cause problems?
    John, Mar 30, 2012
    #1
    1. Advertising

  2. On Fri, 30 Mar 2012 18:15:07 +0300, John wrote:

    > I have a rather lengthy string like this:
    >
    > $page = <<HERE;
    > <input type="hidden" value="%1">
    > </form>
    > ...(a couple hundred html lines)
    > HERE $page=~s/%1/$myvar/ge;
    > print $page;
    >
    >
    > Is there some limit to how long a string the $page variable can hold?


    Limited by available memory. In other words, don't worry about it with
    only a few hunderd lines. Start worrying at a few million, but you
    probably will be fine even then.

    > Another question:
    > The the line $page=~s/%1/$myvar/ge;
    > processes the lengthy string. Will I run into some limitations there?
    > will that cause problems?


    This should be fine, but why the /e modifier?

    Where you can get into problems with regular expressions, is when you
    need to backtrack often. In that case, you can get performance problems
    with huge strings. But as you have no wildcards in your search pattern,
    there is no risk of this.

    And even in the case of an inefficient regular expression on a huge
    string:
    1) with modern fast machines, you probably won't notice anyhow; and
    2) you probably need to read and write that string from/to disk/network,
    which is relatively slow, so you won't notice anyhow.

    HTH,
    M4


    M4
    Martijn Lievaart, Mar 30, 2012
    #2
    1. Advertising

  3. John <> writes:
    > I have a rather lengthy string like this:
    >
    > $page = <<HERE;
    > <input type="hidden" value="%1">
    > </form>
    > ...(a couple hundred html lines)
    > HERE
    > $page=~s/%1/$myvar/ge;
    > print $page;


    'A couple of hundred lines' is not 'lengthy' for an at least remotely
    modern computer.

    > Is there some limit to how long a string the $page variable can hold?
    > Another question:
    > The the line
    > $page=~s/%1/$myvar/ge;
    > processes the lengthy string. Will I run into some limitations there?
    > will that cause problems?


    This roughly amounts to copying the string in sections and inserting
    $myvar instead of a %1 from the original string so the answer should
    be 'no'. In particular, you won't be able to outperform the C code
    which does this substitution copy with anything written in Perl.
    Rainer Weikusat, Mar 30, 2012
    #3
  4. John

    John Guest

    Martijn Lievaart <> wrote:

    >On Fri, 30 Mar 2012 18:15:07 +0300, John wrote:
    >
    >> I have a rather lengthy string like this:
    >>
    >> $page = <<HERE;
    >> <input type="hidden" value="%1">
    >> </form>
    >> ...(a couple hundred html lines)
    >> HERE $page=~s/%1/$myvar/ge;
    >> print $page;
    >>
    >>
    >> Is there some limit to how long a string the $page variable can hold?

    >
    >Limited by available memory. In other words, don't worry about it with
    >only a few hunderd lines. Start worrying at a few million, but you
    >probably will be fine even then.
    >
    >> Another question:
    >> The the line $page=~s/%1/$myvar/ge;
    >> processes the lengthy string. Will I run into some limitations there?
    >> will that cause problems?

    >
    >This should be fine, but why the /e modifier?


    I am not sure about it too. Should it be there and if not please elaborate. I
    took that from sample code.
    John, Mar 30, 2012
    #4
  5. John

    Tim McDaniel Guest

    In article <>,
    John <> wrote:
    >Martijn Lievaart <> wrote:
    >
    >>On Fri, 30 Mar 2012 18:15:07 +0300, John wrote:
    >>> Another question:
    >>> The the line $page=~s/%1/$myvar/ge;
    >>> processes the lengthy string. Will I run into some limitations
    >>> there? will that cause problems?

    >>
    >>This should be fine, but why the /e modifier?

    >
    >I am not sure about it too. Should it be there and if not please
    >elaborate. I took that from sample code.


    To know what it might be useful for, you can look at the
    documentation. In this case, "man perlop", which explains about Perl
    operators. A Google search for
    "s///e" Perl
    turns out not to be bad, which surprises me.

    Anyway, from man perlop, I searched for /e. In less, that's
    /\/e

    I found, in Perl 5.8.8,
    s/PATTERN/REPLACEMENT/egimosx

    ... A "/e" will cause the replacement portion to be treated as a
    full-fledged Perl expression and evaluated right then and there.
    It is, however, syntax checked at compile-time. A second "e"
    modifier will cause the replacement portion to be "eval"ed before
    being run as a Perl expression. ...

    and explanations of g, i, m, o, s, and x. This is Perl 5.8; r and
    maybe other modifiers have been added since.

    The example code you provided showed that the string is HTML. Since
    the right-hand side of the s/// does not require the full power of
    Perl expressions, and since you merely want the variable substituted
    in, you do not want to do /e.

    The first Google hit is man perlrequick. I find it a little more
    helpful:
    http://perldoc.perl.org/perlrequick.html

    The evaluation modifier s///e wraps an eval{...} around the
    replacement string and the evaluated result is substituted for the
    matched substring. Some examples:

    # reverse all the words in a string
    $x = "the cat in the hat";
    $x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"

    # convert percentage to decimal
    $x = "A 39% hit rate";
    $x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"

    --
    Tim McDaniel,
    Tim McDaniel, Mar 30, 2012
    #5
  6. John

    J. Gleixner Guest

    On 03/30/12 12:59, John wrote:

    >>> I have a rather lengthy string like this:
    >>>
    >>> $page =<<HERE;
    >>> <input type="hidden" value="%1">
    >>> </form>
    >>> ...(a couple hundred html lines)
    >>> HERE $page=~s/%1/$myvar/ge;
    >>> print $page;

    [...]

    Looks like the beginning of YATS (Yet Another Template System) :)

    You may want to look at using one of the many Template packages
    already available, which will help you design something a little
    better by following more of a MVC pattern. You would move those
    'couple hundred html lines' out of your program and into another
    file (template). Your program itself will be much shorter and
    whatever you are doing it will be much easier to maintain/enhance.

    http://search.cpan.org/search?query=template&mode=all

    http://en.wikipedia.org/wiki/Model–view–controller
    J. Gleixner, Mar 30, 2012
    #6
  7. John <> wrote:
    >I have a rather lengthy string like this:
    >
    >$page = <<HERE;
    ><input type="hidden" value="%1">
    ></form>
    >...(a couple hundred html lines)
    >HERE
    >$page=~s/%1/$myvar/ge;
    >print $page;
    >
    >
    >Is there some limit to how long a string the $page variable can hold?


    If I remember correctly from some discussions a long time ago in a
    galaxy far away then there is a limit of something like 2GB.
    But maybe that was for a 32-bit system, I don't quite remember.

    jue
    Jürgen Exner, Mar 31, 2012
    #7
  8. On 2012-03-31 00:45, Ben Morrow <> wrote:
    > Quoth Jürgen Exner <>:
    >> John <> wrote:
    >> >
    >> >Is there some limit to how long a string the $page variable can hold?

    >>
    >> If I remember correctly from some discussions a long time ago in a
    >> galaxy far away then there is a limit of something like 2GB.
    >> But maybe that was for a 32-bit system, I don't quite remember.

    >
    > I think there used to be an (unnecessary) 2^32 limit, even on 64-bit
    > systems, but there isn't any more. 5.12 will quite happily create a
    > string longer than that on a 64-bit system (assuming you've got the
    > memory to hold it).


    Same for 5.10.1 and 5.8.8. Haven't tried older versions.

    hp



    --
    _ | Peter J. Holzer | Deprecating human carelessness and
    |_|_) | Sysadmin WSR | ignorance has no successful track record.
    | | | |
    __/ | http://www.hjp.at/ | -- Bill Code on
    Peter J. Holzer, Mar 31, 2012
    #8
  9. Retraction of a claim about File::Slurp (was: string variable to hold a lengthy string)

    Rainer Weikusat <> writes:

    [...]

    > This roughly amounts to copying the string in sections and inserting
    > $myvar instead of a %1 from the original string so the answer should
    > be 'no'.


    This obviously implies that my older claim that File::Slurp would
    trigger use of an O(n*n) algortihm for 'Windows text file support' is
    wrong: It would be true for an in-place substitution performed on the
    input string because each individual substitution would require a copy
    of the tail string and the total running time would be proportional to
    the sum of the lengths of all tail strings which needed to be
    copied. But at least for simple substitutions, perl doesn't do that
    and while there's theoretically a similar problem with sequences of
    copying reallocs, at least for my simple set of test cases and the
    realloc implementation of the system I was using, this didn't happen.

    OTOH, the 'linearity' of the Perl-level algorithm rests on
    undocumented properties of the pp_subst implementation, which, in
    turn, relies on undocumented properties of the system realloc, and
    this is - in my opinion - extremely bad style and it is also prone to
    cause 'maintenance surprises' because Perl will do an in-place
    subsitutation in certain cases (read: The code is there and it is not
    immediately obvious under which circumstance it is going to be
    executed) and it is pretty certain that people won't do an in depth
    analysis of this part of the perl implementation before changing a
    s/// expression.
    Rainer Weikusat, Mar 31, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RWQgQ2hpdQ==?=

    Navigation for a lengthy page

    =?Utf-8?B?RWQgQ2hpdQ==?=, Oct 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    370
    Jeffrey Palermo [MCP]
    Oct 30, 2004
  2. Replies:
    4
    Views:
    444
  3. Ken Varn
    Replies:
    6
    Views:
    809
    Ken Varn
    Apr 28, 2005
  4. =?Utf-8?B?UkZQdWxzaWZlcg==?=

    Rendering very lengthy

    =?Utf-8?B?UkZQdWxzaWZlcg==?=, May 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    328
    =?Utf-8?B?UkZQdWxzaWZlcg==?=
    May 16, 2005
  5. John Reye
    Replies:
    14
    Views:
    761
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page