methods to insert/substitute blocks of text? 
Author Message
 methods to insert/substitute blocks of text?

I would like to get some thoughts from the crowd on different methods to
substitute blocks of text into templates. I have "surfed" up a couple
posts mentioning this sort of functionality in a couple different
modules, but have not tested to see which one is the easiest to
implement.... I'd like to hear from some people who have done this type
of thing. It would be great if some discussion got started that could
compare and contrast the available methods.... as long as it doesn't
become a religious war. ;)

The project is to substitute small chunks of dynamically generated text
into largely static HTML source. i.e. create drop-down choice lists
dynamically.

I'd *like* to leave the HTML source page as working source so that
non-perl people can edit the basic format of the page (as long as they
don't delete start/end tags or whatever is required for substitutions.

Please let me know what modules you think would be best for this sort of
substitution work! Also, if you have any simple examples, or tutorial
type material online, let me know!

thanx,

Dan



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?

Quote:

> I would like to get some thoughts from the crowd on different methods to
> substitute blocks of text into templates.

There must be s...loads of different ways to do this.
On my webpage there is a script which has the static webstuff stored in the
__DATA__ entry of the script. It then outputs that together with some
dynamically generated stuff in the middle. Handy if you want everything to be
together! Check it out through my sig!

--
____________________________________________________________
Frank Quednau              
http://www.surrey.ac.uk/~me51fq
________________________________________________



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:I would like to get some thoughts from the crowd on different methods to
:substitute blocks of text into templates.

From the Perl Cookbook:

    To expand only variable references, use this template() function:

        %fields = (
                    username => $whats_his_name,
                    count    => $login_count,
                    total    => $minute_used,
        );

        print template("/home/httpd/templates/simple.template", \%fields);

    sub template {

        my $text;
        local $/;                   # slurp mode (undef)
        local *F;                   # create local filehandle
        open(F, "< $filename")      || return;
        $text = <F>;                  # read whole file
        close(F);                   # ignore retval
        # replace quoted words with value in %$fillings hash
        $text =~ s{ %% ( .*? ) %% }
                  { exists( $fillings->{$1} )
                          ? $fillings->{$1}
                          : ""
                  }gsex;
        return $text;
    }

On a data file like this

    <!-- simple.template for internal template() function -->
    <HTML><HEAD><TITLE>Report for %%username%%</TITLE></HEAD>
    <BODY><H1>Report for %%username%%</H1>
    %%username%% logged in %%count%% times, for a total of %%total%% minutes.

--tom
--
The human mind ordinarily operates at only ten percent of its capacity
--the rest is overhead for the operating system.



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?

Quote:
>I would like to get some thoughts from the crowd on different methods to
>substitute blocks of text into templates.

There are several templating modules avilable from CPAN.

Quote:
>I'd *like* to leave the HTML source page as working source so that
>non-perl people can edit the basic format of the page (as long as they
>don't delete start/end tags or whatever is required for substitutions.

Text::MetaText is probably what you want.  

Quote:
>Please let me know what modules you think would be best for this sort of
>substitution work! Also, if you have any simple examples, or tutorial
>type material online, let me know!

Check out the documentation which comes as part of the distribution.

  http://www.perl.com/CPAN/modules/by-module/Text/Text-MetaText-0.19.ta...

As the author of Text::MetaText I'm naturally biased, but you can look in
the "Features" file in the distribution which talks about some of the pros
and cons of other templating modules.

A

--




Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?
Quote:


> > I would like to get some thoughts from the crowd on different methods to
> > substitute blocks of text into templates.

> There must be s...loads of different ways to do this.
> On my webpage there is a script which has the static webstuff stored in the
> __DATA__ entry of the script. It then outputs that together with some
> dynamically generated stuff in the middle. Handy if you want everything to be
> together! Check it out through my sig!

--------

definitely one way, but I am hoping to leave the HTML source as a .htm
file so that it can be viewed and editted by a non-perl person with
regular HTML editors.

Dan



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?
This is pretty cool,and I think that but changing the tags used I might
even be able to leave the HTML template a working html page. There are a
couple things I'm going to have to read more about! In case anyone wants
to explain a little, I've put my questions below... I'm a novice, so be
patient.  ;)

Dan

Quote:

>  [courtesy cc of this posting sent to cited author via email]

> In comp.lang.perl.misc,

> :I would like to get some thoughts from the crowd on different methods to
> :substitute blocks of text into templates.

> From the Perl Cookbook:

>     To expand only variable references, use this template() function:

>         %fields = (
>                     username => $whats_his_name,
>                     count    => $login_count,
>                     total    => $minute_used,
>         );

>         print template("/home/httpd/templates/simple.template", \%fields);

>     sub template {


---

anyone explain?

Quote:
>         my $text;
>         local $/;                   # slurp mode (undef)

---
what's this? any suggested reading? i.e. why do we need to worry about
the input record separator, or is that not what it is here?

Quote:
>         local *F;                   # create local filehandle
>         open(F, "< $filename")      || return;
>         $text = <F>;                # read whole file
>         close(F);                   # ignore retval

---
whups, I got lost here... doesn't reading the the whole file into a
string "lose" the line returns... how will we get them back?

Quote:
>         # replace quoted words with value in %$fillings hash
>         $text =~ s{ %% ( .*? ) %% }
>                   { exists( $fillings->{$1} )
>                           ? $fillings->{$1}
>                           : ""
>                   }gsex;

---
can someone go slow and explain what each line of this expression does?

Quote:
>         return $text;
>     }

---
ok, I see that this returns the now-substituted text, and I'm assuming
that since the \n weren't chomped out when the file was read into $text,
that they pop back out when returned. correct?

- Show quoted text -

Quote:

> On a data file like this

>     <!-- simple.template for internal template() function -->
>     <HTML><HEAD><TITLE>Report for %%username%%</TITLE></HEAD>
>     <BODY><H1>Report for %%username%%</H1>
>     %%username%% logged in %%count%% times, for a total of %%total%% minutes.

> --tom
> --
> The human mind ordinarily operates at only ten percent of its capacity
> --the rest is overhead for the operating system.



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?
looks like a good possability, I'll have to go read more about it...
Andy, please feel free to expound on the package's many pros and ease of
use for all of our education of course!  ;)
Quote:


> >I would like to get some thoughts from the crowd on different methods to
> >substitute blocks of text into templates.

> There are several templating modules avilable from CPAN.

> >I'd *like* to leave the HTML source page as working source so that
> >non-perl people can edit the basic format of the page (as long as they
> >don't delete start/end tags or whatever is required for substitutions.

> Text::MetaText is probably what you want.

> >Please let me know what modules you think would be best for this sort of
> >substitution work! Also, if you have any simple examples, or tutorial
> >type material online, let me know!

> Check out the documentation which comes as part of the distribution.

>   http://www.perl.com/CPAN/modules/by-module/Text/Text-MetaText-0.19.ta...

> As the author of Text::MetaText I'm naturally biased, but you can look in
> the "Features" file in the distribution which talks about some of the pros
> and cons of other templating modules.

> A

> --





Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?

Quote:
>This is pretty cool,and I think that but changing the tags used I might
>even be able to leave the HTML template a working html page. There are a
>couple things I'm going to have to read more about! In case anyone wants
>to explain a little, I've put my questions below... I'm a novice, so be
>patient.  ;)

time for your homework ;)

Quote:


>anyone explain?

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

Quote:
>>         my $text;
>>         local $/;                   # slurp mode (undef)
>---
>what's this? any suggested reading? i.e. why do we need to worry about
>the input record separator, or is that not what it is here?

think about what the input record separator means, then see

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

Quote:
>>         local *F;                   # create local filehandle
>>         open(F, "< $filename")      || return;
>>         $text = <F>;                # read whole file
>>         close(F);                   # ignore retval
>whups, I got lost here... doesn't reading the the whole file into a
>string "lose" the line returns... how will we get them back?

no data are lost.  data are data. see

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

Quote:
>>         # replace quoted words with value in %$fillings hash
>>         $text =~ s{ %% ( .*? ) %% }
>>                   { exists( $fillings->{$1} )
>>                           ? $fillings->{$1}
>>                           : ""
>>                   }gsex;
>can someone go slow and explain what each line of this expression does?

it's the s/// operator with {} as delimiters and an eval for the
replacement string.

see perlre and

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

Quote:
>>         return $text;
>>     }
>---
>ok, I see that this returns the now-substituted text, and I'm assuming
>that since the \n weren't chomped out when the file was read into $text,
>that they pop back out when returned. correct?

no, $test had the newlines all along.  see

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

__Other Resources:__

the Perl man page

the Perl FAQ

<URL:http://www.perl.com>

Learning Perl
Randal L. Schwartz & Tom Christiansen
ISBN 1-56592-284-0
<URL:http://www.oreilly.com>

Programming Perl
Larry Wall, Tom Christiansen & Randal L. Schwartz
ISBN 1-56592-149-6
<URL:http://www.oreilly.com>

--

CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
oh, and "Learning Perl".



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?


...
Quote:
> In case anyone wants
> to explain a little, I've put my questions below... I'm a novice, so be
> patient.  ;)

...

Well, since you asked so nicely...

Quote:

...
> >     sub template {

> ---

> anyone explain?

This statement assigns a list to a list, which is done scalar by scalar.  
The first argument $_[0] is assigned to $filename; the second argument
$_[1] is assigned to $fillings.

Quote:
> >         my $text;
> >         local $/;                   # slurp mode (undef)
> ---
> what's this? any suggested reading? i.e. why do we need to worry about
> the input record separator, or is that not what it is here?

Yes, it is, and we don't want no s{*filter*}keen' input record separator (in
order to handle parameter substitutions that span more than one line).  
See the discussion of $/ in perlvar.

Quote:
> >         local *F;                   # create local filehandle
> >         open(F, "< $filename")      || return;
> >         $text = <F>;                # read whole file
> >         close(F);                   # ignore retval
> ---
> whups, I got lost here... doesn't reading the the whole file into a
> string "lose" the line returns... how will we get them back?

No, they're still there.  Each one is in the string as the single
character "\n", regardless of their external representation.

Quote:
> >         # replace quoted words with value in %$fillings hash
> >         $text =~ s{ %% ( .*? ) %% }
> >                   { exists( $fillings->{$1} )
> >                           ? $fillings->{$1}
> >                           : ""
> >                   }gsex;
> ---
> can someone go slow and explain what each line of this expression does?

Extract into $1 the fewest number of characters between pairs of '%%' and
'%%' (including none; the * could as well have been a +).  If that string
is a key of the hash referred to by $fillings, replace the matched string
by the value of the hash member; otherwise replace the matched string by
nothing.  [Even more compactly: { exists $fillings->{$1} && $fillings-

Quote:
>{$1} } .]

Do it over and over ('g'), allowing the '.' to match the embedded "\n"
characters ('s'), evaluating the second part of the substitution as a
Perl expression ('e'), and allowing for white space and comments in the
regular expression ('x').  [The order of these four options is
irrelevant.  Hmmm...]

Quote:
> >         return $text;
> >     }
> ---
> ok, I see that this returns the now-substituted text, and I'm assuming
> that since the \n weren't chomped out when the file was read into $text,
> that they pop back out when returned. correct?

No, they were there all along.

All this is somewhat beyond 'Learning Perl' but certainly clear enough
from 'Programming Perl' or perlre.

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.*-*-*.com/



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?

Quote:

> time for your homework ;)
>... see

> Learning Perl
> Randal L. Schwartz & Tom Christiansen
> ISBN 1-56592-284-0
> <URL:http://www.oreilly.com>

--------------

ok ok, I get the point. The standard RFM response is a little daunting
to those of us who HAVE been reading desperately and are still a little
confused and looking for a little straightforward help... I realize that
if I read every Oreilly book out there I'd probably have the answer! The
question is a matter of when....

If all you want to do if point me to a book, I'd find it more helpful if
you could give a hint like maybe what chapter, or page you you don't
want to re-type.

Dan



Sat, 23 Dec 2000 03:00:00 GMT  
 methods to insert/substitute blocks of text?

Quote:


> ...
> > In case anyone wants
> > to explain a little, I've put my questions below... I'm a novice, so be
> > patient.  ;)
> ...

> Well, since you asked so nicely...

--------------

THANK YOU LARRY! Your response is VERY helpful and instructive. I'm sure
that it probably helped point out some of the more subtle things to
newbies like me. I appreciate the time and clarity with which you've
explained the constructs I hadn't seen in the beginners books!

Quote:
> > >        print template("/home/httpd/templates/simple.template", \%fields);

> > >     sub template {

> > ---

> > anyone explain?

> This statement assigns a list to a list, which is done scalar by scalar.
> The first argument $_[0] is assigned to $filename; the second argument
> $_[1] is assigned to $fillings.

--------------
ok, I'm sorta getting this, but just to make sure... I can see how the
scalar "/home/httpd/templates/simple.template" maps right into
$filename, but am a little confused on how the %fields list gets
flattened into the string $fillings ??? Why wouldn't the assignment have
to match types like:

?

Quote:

> > >         my $text;
> > >         local $/;                   # slurp mode (undef)
> > ---
> > what's this? any suggested reading? i.e. why do we need to worry about
> > the input record separator, or is that not what it is here?

> Yes, it is, and we don't want no s{*filter*}keen' input record separator (in
> order to handle parameter substitutions that span more than one line).
> See the discussion of $/ in perlvar.

----------------
okdoky.... read I shall.

Quote:

> > >         # replace quoted words with value in %$fillings hash
> > >         $text =~ s{ %% ( .*? ) %% }
> > >                   { exists( $fillings->{$1} )
> > >                           ? $fillings->{$1}
> > >                           : ""
> > >                   }gsex;
> > ---
> > can someone go slow and explain what each line of this expression does?

> Extract into $1 the fewest number of characters between pairs of '%%' and
> '%%' (including none; the * could as well have been a +).  If that string
> is a key of the hash referred to by $fillings, replace the matched string
> by the value of the hash member; otherwise replace the matched string by
> nothing.  [Even more compactly: { exists $fillings->{$1} && $fillings-
> >{$1} } .]

> Do it over and over ('g'), allowing the '.' to match the embedded "\n"
> characters ('s'), evaluating the second part of the substitution as a
> Perl expression ('e'), and allowing for white space and comments in the
> regular expression ('x').  [The order of these four options is
> irrelevant.  Hmmm...]

---------
great, thank you!  .... I like the gsex order, it will be easy to
remember... ;)

Quote:
> All this is somewhat beyond 'Learning Perl' but certainly clear enough
> from 'Programming Perl' or perlre.

---------
well I'm barely at "learning perl" when it comes to some of the subtle
syntax... so thanx for explaining!

Dan



Sat, 23 Dec 2000 03:00:00 GMT  
 
 [ 37 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. insert method of Text widget

2. Text Insert Method

3. split and substitute, substitute, substitute

4. Inserting text top of plain/text MIME section

5. Inserting text in text file

6. TK::Text - Inserting text

7. Text->insert delayed output to text widget

8. substituting string of text

9. Substituting variables in a text file

10. Need help substituting text except when in an HTML anchor

11. substituting text in a var (beginner question)

12. inserted text

 

 
Powered by phpBB® Forum Software