Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string variable to hold a lengthy string

Reply
Thread Tools

string variable to hold a lengthy string

 
 
John
Guest
Posts: n/a
 
      03-30-2012
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?
 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-30-2012
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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      03-30-2012
John <(E-Mail Removed)> 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.

 
Reply With Quote
 
John
Guest
Posts: n/a
 
      03-30-2012
Martijn Lievaart <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      03-30-2012
In article <(E-Mail Removed)>,
John <(E-Mail Removed)> wrote:
>Martijn Lievaart <(E-Mail Removed)> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      03-30-2012
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%E...0%93controller
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-31-2012
John <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-31-2012
On 2012-03-31 00:45, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth Jürgen Exner <(E-Mail Removed)>:
>> John <(E-Mail Removed)> 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.
| | | (E-Mail Removed) |
__/ | http://www.hjp.at/ | -- Bill Code on (E-Mail Removed)
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      03-31-2012
Rainer Weikusat <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generates integer literal of the largest value a variable can hold.Macro takes only variable-name. John Reye C Programming 14 05-08-2012 04:40 AM
Rendering very lengthy =?Utf-8?B?UkZQdWxzaWZlcg==?= ASP .Net 0 05-16-2005 08:26 PM
How do I cancel a lengthy process on server side? Ken Varn ASP .Net 6 04-28-2005 07:49 PM
Problem with lengthy process atefshehata@hotmail.com ASP .Net 4 03-01-2005 09:44 AM
Navigation for a lengthy page =?Utf-8?B?RWQgQ2hpdQ==?= ASP .Net 3 10-30-2004 01:24 AM



Advertisments