[16768] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4180 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 30 18:10:45 2000

Date: Wed, 30 Aug 2000 15:10:28 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <967673427-v9-i4180@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 30 Aug 2000     Volume: 9 Number: 4180

Today's topics:
    Re: Glob errors? <mmesarch@wcom.net>
        help..I don't know what is this??? eduardo_m@my-deja.com
    Re: help..I don't know what is this??? <jeff@vpservices.com>
        How to launch a windows application from my script? <rocket@acme.com>
    Re: How to launch a windows application from my script? <lauren_smith13@hotmail.com>
    Re: How to use a template with perl --- novice question <jevon@islandtelecom.com>
    Re: How to use a template with perl --- novice question <timewarp@shentel.net>
    Re: How to use a template with perl --- novice question (brian d foy)
    Re: How to use a template with perl --- novice question <agustinchernitsky@altavista.com>
    Re: How to use a template with perl --- novice question <agustinchernitsky@altavista.com>
    Re: How to use a template with perl --- novice question <jevon@islandtelecom.com>
    Re: How to use a template with perl --- novice question (brian d foy)
    Re: How to use a template with perl --- novice question <bart.lateur@skynet.be>
    Re: How to use a template with perl --- novice question <lr@hpl.hp.com>
    Re: interchanging variables (Abigail)
    Re: interchanging variables (Craig Berry)
    Re: interchanging variables <peckert@epicrealm.com>
    Re: interchanging variables <lr@hpl.hp.com>
    Re: interchanging variables <bart.lateur@skynet.be>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 30 Aug 2000 17:24:18 -0400
From: "Mike Mesarch" <mmesarch@wcom.net>
Subject: Re: Glob errors?
Message-Id: <8oju2r$gvi$1@sshuraab-i-1.production.compuserve.com>

"jason" <elephant@squirrelgroup.com> wrote in message
news:MPG.141718743373b61298972c@localhost...
"use either File::DosGlob as perlwin32 recommends"
Thanks!  That took care of it.
-Mike

> Mike Mesarch <mmesarch@wcom.net> wrote ..
> >I am running perl on an NT box and this glob line is causing a problem.
As
> >a note I stole the code from a script I wrote in Solaris which worked
fine.
> >I am essentially just running a script to recurse through directories.
If
> >it is a directory I want it to do something special and recurse through
it.
> >The problem lies when a directory has a space in the name.  For example
the
> >directory  M:\Compaq Support now becomes M:./Compaq.  Below is the line
and
> >the sub causing the problem.  Any help would be appreciated.  As you can
> >tell by the code I am no guru.
>
> glob on Win32 isn't very good .. it has to spawn a new process and is
> messy and yucky *8^) .. sorry - can't be bothered giving technical
> reasons not to use it .. read the perlwin32 section of the manual for a
> more detailed description
>
> use either File::DosGlob as perlwin32 recommends .. or from what you're
> saying your task could easily be solved with File::Find (again covered
> in the documentation)
>
>   perldoc perlwin32
>   perldoc File::DosGlob
>   perldoc File::Find
>
> --
>   jason -- elephant@squirrelgroup.com --




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

Date: Wed, 30 Aug 2000 21:44:21 GMT
From: eduardo_m@my-deja.com
Subject: help..I don't know what is this???
Message-Id: <8ojv7h$eb2$1@nnrp1.deja.com>

Hello everyone....

I'm programming in DBI and I looked into several tutorials, and I have
found one that says:

$sth->execute();

if ($@) {
    $dbh->rollback();
}

I know how works this, but I want to know what the "$@" means...Is
something like a way to know if an error has ocurred????....

Thanks in advance...


Eduardo


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Wed, 30 Aug 2000 15:01:26 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: help..I don't know what is this???
Message-Id: <39AD8436.2A5F896D@vpservices.com>


eduardo_m@my-deja.com wrote:
> 
> Hello everyone....
> 
> I'm programming in DBI and I looked into several tutorials, and I have
> found one that says:
> 
> $sth->execute();
> 
> if ($@) {
>     $dbh->rollback();
> }
> 
> I know how works this, but I want to know what the "$@" means...Is
> something like a way to know if an error has ocurred????....
> 

It holds the error from an eval.  Try perldoc -f eval


-- 
Jeff


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

Date: Wed, 30 Aug 2000 14:39:03 -0700
From: JakeRocket <rocket@acme.com>
Subject: How to launch a windows application from my script?
Message-Id: <BC1F83528AD55F84.B078699A8D8408B8.83BAFFCDD98160BC@lp.airnews.net>

I'm working exclusively in a Win32 environment, so keep that in mind.
I am writing to an Output file that I want to launch in Windows
Notepad as soon as my script is done.  Does perl support this, and if
so how?

-Jeff


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

Date: Wed, 30 Aug 2000 14:47:29 -0700
From: "Lauren Smith" <lauren_smith13@hotmail.com>
Subject: Re: How to launch a windows application from my script?
Message-Id: <8ojv9q$cfd$1@brokaw.wa.com>


JakeRocket <rocket@acme.com> wrote in message
news:BC1F83528AD55F84.B078699A8D8408B8.83BAFFCDD98160BC@lp.airnews.net...
> I'm working exclusively in a Win32 environment, so keep that in mind.
> I am writing to an Output file that I want to launch in Windows
> Notepad as soon as my script is done.  Does perl support this, and if
> so how?

#!perl -w
system ("notepad.exe");

or alternatively,

exec ("notepad.exe");

See the documentation for exec() and system().  Type the following lines
into your command window:

perldoc -f system
perldoc -f exec

Lauren
--
print grep ord $_,map{y/a-zA-Z//d;$x.="+ $_";chr(eval $x)}
'J74u43-s2tA1-84n33o45th1er5-12-P3e13-82r48l21H13-a6-76
c40k25er2wx8-y6z13-81'=~m#([^!\n]{3})#g#tr/-0-9//d;print




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

Date: Wed, 30 Aug 2000 19:45:31 GMT
From: "Jevon MacDonald" <jevon@islandtelecom.com>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <vvdr5.63$KJ3.31615@sapphire.mtt.net>

Hey Agustin,
    I wrote a neat templating system while doing some webwork a year or so
ago.. I enjoyed this code and I think it is still alright today.

There are three main components to the templating system -
The first is the template() sub..
-----------BEGIN TEMPLATE()----------------
sub template {
    my ($filename, $fillings) = @_;
    my $text;
    local $/;
    local *F;
    open(F, "<$filename") || die "no go on opening the template [I NEED
ONE!]";
        $text = <F>;
        $text =~ s{ %% ( .*? ) %% } { exists( $fillings->{$1} )
?$fillings->{$1} :""   }gsex;
     close(F);

    return $text;
}
-----------END TEMPLATE()----------------

Throw that at the top or the bottom of your code (or in a separate file!).
Then, Give it a Hash Array to work with.I use
$fields{'INDICATOR'}="foobarisoldcode";
You specified a need for $fields{'TEXTHERE'}=$scalar;

Next you invoke the templating sub on the data conatiner of your choice (A
template from a file I assume)..
eg.
-----------BEGIN SAMPLE----------
1: open(INCL, ">" . $path . "index.html");
2:     print INCL template ($path . "/templ/template.html", \%fields);
3: close(INCL);
-----------END SAMPLE----------

Line 1: This line opens a file to OUTPUT TO
Line 2: This  invoke the template sub, uses the ($path .
"admin/templ/tem.html") to tell it where to get the Template file from
            and the last part tells the Template sub to use the %fields hash
array for it's parsing needs.
Line 3: Closes the file it outputted to..

Alternativly, this code [print "template($path . "admin/templ/tem.html",
\%fields)";] would print the results to the screen/browser... (STDOUT)...
The main this to remember is $fields{'ME'}="Jevon"; will replace all
instances of %%ME%% it finds in the template file.. (Note the %% on either
side of the "variable"...)

Hope this helps., If you use it: Let me know.. It is a slightly bloated
solution for this specific problem but people might enjoy implementing it
for all sorts of stuff. It is amazingly versitile. A perfect alternative to
CGI.pm's and other solutions.. I love it for web-templating.. I had done it
all with HEX and machine code but perl did a faster job!!!, How cool is
that?

 Anyway, Have fun with it,
            Jevon


"Agustin Chernitsky" <agustinchernitsky@altavista.com> wrote in message
news:8oj8od$b6ts4$1@ID-48235.news.cis.dfn.de...
> Hi,
>
>     I created a scripts (an orders script) that uses an HTML file as a
main
> template. This HTML has a text (ie TEXTHERE) that indicates the program to
> insert other HTML code in that line. This program is made up of 3 parts:
> main, an order summary and a contract info.
>
> These 3 parts use the same template. Now as I'm not an expert but I have
> read Larry Wall's Learning Perl and Programming Perl, I was able to make
> this program work. What I need now is to optimize the code and for that I
> need some experience. So, maybe someone here could help me.
>
> The code opens the template on all the 3 parts (I have 3 open commands in
> the program for the same file). I was thinking on placing all that on a
sub
> routine. What I don't is how to make the  program open the template and
when
> it detects the TEXTHERE, make it insert the new HTML code and after that,
> finish the template (imagine that the TEXTHERE is in the middle of the
> HTML).
>
> Does anyone know how to handle this?
>
> Thanks!!!
>
> --
> Agustín Chernitsky
> InterEmpresa
>
> E-mail:       agustinchernitsky@altavista.com
> ICQ:          20020826
>
>




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

Date: Wed, 30 Aug 2000 15:36:27 -0400
From: Albert Dewey <timewarp@shentel.net>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <39AD623B.8DBB13C6@shentel.net>

Agustin Chernitsky wrote:

> Hi,
>
>     I created a scripts (an orders script) that uses an HTML file as a main
> template. This HTML has a text (ie TEXTHERE) that indicates the program to
> insert other HTML code in that line. This program is made up of 3 parts:
> main, an order summary and a contract info.
>
> These 3 parts use the same template. Now as I'm not an expert but I have
> read Larry Wall's Learning Perl and Programming Perl, I was able to make
> this program work. What I need now is to optimize the code and for that I
> need some experience. So, maybe someone here could help me.
>
> The code opens the template on all the 3 parts (I have 3 open commands in
> the program for the same file). I was thinking on placing all that on a sub
> routine. What I don't is how to make the  program open the template and when
> it detects the TEXTHERE, make it insert the new HTML code and after that,
> finish the template (imagine that the TEXTHERE is in the middle of the
> HTML).
>
> Does anyone know how to handle this?
>
> Thanks!!!
>
> --
> Agustín Chernitsky
> InterEmpresa
>
> E-mail:       agustinchernitsky@altavista.com
> ICQ:          20020826

Agustín -

First load the entire template in a variable, you can call this $webpage. Next,
load the different insertable contents into a set of variables such as $newtext1
- 3. Then, one after another, replace the content markers (TEXTHERE1 - 3) in
this template variable using the following lines of code - (the 'g' is optional
in your case and is primarily used to do a global replacement of the chosen
marker)

$webpage =~ s/TEXTHERE1/$newtext1/g;
$webpage =~ s/TEXTHERE2/$newtext2/g;
$webpage =~ s/TEXTHERE3/$newtext3/g;

Then print the web page to the user with -

print $webpage;

You're done.

Albert Dewey


--
@i = ('a' .. 'z', 'A' .. 'Z', ' ');
print "$i[35]$i[20]$i[18]$i[19]$i[52]$i[26]$i[13]$i[14]";
print "$i[19]$i[7]$i[4]$i[17]$i[52]$i[41]$i[4]$i[17]";
print "$i[11]$i[52]$i[33]$i[0]$i[2]$i[10]$i[4]$i[17]";




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

Date: Wed, 30 Aug 2000 16:20:02 -0400
From: brian@smithrenaud.com (brian d foy)
Subject: Re: How to use a template with perl --- novice question
Message-Id: <brian-ya02408000R3008001620020001@news.panix.com>

In article <39AD623B.8DBB13C6@shentel.net>, Albert Dewey <timewarp@shentel.net> posted:

> First load the entire template in a variable,

that could take up a lot of memory. you should consider if you really
need to do that to accomplish the task (you probably don't).

-- 
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>


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

Date: Wed, 30 Aug 2000 17:21:49 -0300
From: "Agustin Chernitsky" <agustinchernitsky@altavista.com>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <8ojqfe$assfo$1@ID-48235.news.cis.dfn.de>

This is interesting. Brian, what other solution then?

"brian d foy" <brian@smithrenaud.com> wrote in message
news:brian-ya02408000R3008001620020001@news.panix.com...
In article <39AD623B.8DBB13C6@shentel.net>, Albert Dewey
<timewarp@shentel.net> posted:

> First load the entire template in a variable,

that could take up a lot of memory. you should consider if you really
need to do that to accomplish the task (you probably don't).

--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>




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

Date: Wed, 30 Aug 2000 17:22:31 -0300
From: "Agustin Chernitsky" <agustinchernitsky@altavista.com>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <8ojqgt$b17i2$1@ID-48235.news.cis.dfn.de>

Jevon,

    Thanks for the code. It is really interesting. What I don't get is if it
actually creates a new file. Does it? I only wish to output to the browser.

Thanks!

"Jevon MacDonald" <jevon@islandtelecom.com> wrote in message
news:vvdr5.63$KJ3.31615@sapphire.mtt.net...
Hey Agustin,
    I wrote a neat templating system while doing some webwork a year or so
ago.. I enjoyed this code and I think it is still alright today.

There are three main components to the templating system -
The first is the template() sub..
-----------BEGIN TEMPLATE()----------------
sub template {
    my ($filename, $fillings) = @_;
    my $text;
    local $/;
    local *F;
    open(F, "<$filename") || die "no go on opening the template [I NEED
ONE!]";
        $text = <F>;
        $text =~ s{ %% ( .*? ) %% } { exists( $fillings->{$1} )
?$fillings->{$1} :""   }gsex;
     close(F);

    return $text;
}
-----------END TEMPLATE()----------------

Throw that at the top or the bottom of your code (or in a separate file!).
Then, Give it a Hash Array to work with.I use
$fields{'INDICATOR'}="foobarisoldcode";
You specified a need for $fields{'TEXTHERE'}=$scalar;

Next you invoke the templating sub on the data conatiner of your choice (A
template from a file I assume)..
eg.
-----------BEGIN SAMPLE----------
1: open(INCL, ">" . $path . "index.html");
2:     print INCL template ($path . "/templ/template.html", \%fields);
3: close(INCL);
-----------END SAMPLE----------

Line 1: This line opens a file to OUTPUT TO
Line 2: This  invoke the template sub, uses the ($path .
"admin/templ/tem.html") to tell it where to get the Template file from
            and the last part tells the Template sub to use the %fields hash
array for it's parsing needs.
Line 3: Closes the file it outputted to..

Alternativly, this code [print "template($path . "admin/templ/tem.html",
\%fields)";] would print the results to the screen/browser... (STDOUT)...
The main this to remember is $fields{'ME'}="Jevon"; will replace all
instances of %%ME%% it finds in the template file.. (Note the %% on either
side of the "variable"...)

Hope this helps., If you use it: Let me know.. It is a slightly bloated
solution for this specific problem but people might enjoy implementing it
for all sorts of stuff. It is amazingly versitile. A perfect alternative to
CGI.pm's and other solutions.. I love it for web-templating.. I had done it
all with HEX and machine code but perl did a faster job!!!, How cool is
that?

 Anyway, Have fun with it,
            Jevon


"Agustin Chernitsky" <agustinchernitsky@altavista.com> wrote in message
news:8oj8od$b6ts4$1@ID-48235.news.cis.dfn.de...
> Hi,
>
>     I created a scripts (an orders script) that uses an HTML file as a
main
> template. This HTML has a text (ie TEXTHERE) that indicates the program to
> insert other HTML code in that line. This program is made up of 3 parts:
> main, an order summary and a contract info.
>
> These 3 parts use the same template. Now as I'm not an expert but I have
> read Larry Wall's Learning Perl and Programming Perl, I was able to make
> this program work. What I need now is to optimize the code and for that I
> need some experience. So, maybe someone here could help me.
>
> The code opens the template on all the 3 parts (I have 3 open commands in
> the program for the same file). I was thinking on placing all that on a
sub
> routine. What I don't is how to make the  program open the template and
when
> it detects the TEXTHERE, make it insert the new HTML code and after that,
> finish the template (imagine that the TEXTHERE is in the middle of the
> HTML).
>
> Does anyone know how to handle this?
>
> Thanks!!!
>
> --
> Agustín Chernitsky
> InterEmpresa
>
> E-mail:       agustinchernitsky@altavista.com
> ICQ:          20020826
>
>






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

Date: Wed, 30 Aug 2000 20:32:50 GMT
From: "Jevon MacDonald" <jevon@islandtelecom.com>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <Sber5.67$KJ3.35079@sapphire.mtt.net>

Hey Agustin,

    Check out the paragraph after the line-by-line description... I show a
little snippet there that should do the trick!

    Hope you are on the road to a nice site!

    -Jevon

"Agustin Chernitsky" <agustinchernitsky@altavista.com> wrote in message
news:8ojqgt$b17i2$1@ID-48235.news.cis.dfn.de...
> Jevon,
>
>     Thanks for the code. It is really interesting. What I don't get is if
it
> actually creates a new file. Does it? I only wish to output to the
browser.
>
> Thanks!
>
> "Jevon MacDonald" <jevon@islandtelecom.com> wrote in message
> news:vvdr5.63$KJ3.31615@sapphire.mtt.net...
> Hey Agustin,
>     I wrote a neat templating system while doing some webwork a year or so
> ago.. I enjoyed this code and I think it is still alright today.
>
> There are three main components to the templating system -
> The first is the template() sub..
> -----------BEGIN TEMPLATE()----------------
> sub template {
>     my ($filename, $fillings) = @_;
>     my $text;
>     local $/;
>     local *F;
>     open(F, "<$filename") || die "no go on opening the template [I NEED
> ONE!]";
>         $text = <F>;
>         $text =~ s{ %% ( .*? ) %% } { exists( $fillings->{$1} )
> ?$fillings->{$1} :""   }gsex;
>      close(F);
>
>     return $text;
> }
> -----------END TEMPLATE()----------------
>
> Throw that at the top or the bottom of your code (or in a separate file!).
> Then, Give it a Hash Array to work with.I use
> $fields{'INDICATOR'}="foobarisoldcode";
> You specified a need for $fields{'TEXTHERE'}=$scalar;
>
> Next you invoke the templating sub on the data conatiner of your choice (A
> template from a file I assume)..
> eg.
> -----------BEGIN SAMPLE----------
> 1: open(INCL, ">" . $path . "index.html");
> 2:     print INCL template ($path . "/templ/template.html", \%fields);
> 3: close(INCL);
> -----------END SAMPLE----------
>
> Line 1: This line opens a file to OUTPUT TO
> Line 2: This  invoke the template sub, uses the ($path .
> "admin/templ/tem.html") to tell it where to get the Template file from
>             and the last part tells the Template sub to use the %fields
hash
> array for it's parsing needs.
> Line 3: Closes the file it outputted to..
>
> Alternativly, this code [print "template($path . "admin/templ/tem.html",
> \%fields)";] would print the results to the screen/browser... (STDOUT)...
> The main this to remember is $fields{'ME'}="Jevon"; will replace all
> instances of %%ME%% it finds in the template file.. (Note the %% on either
> side of the "variable"...)
>
> Hope this helps., If you use it: Let me know.. It is a slightly bloated
> solution for this specific problem but people might enjoy implementing it
> for all sorts of stuff. It is amazingly versitile. A perfect alternative
to
> CGI.pm's and other solutions.. I love it for web-templating.. I had done
it
> all with HEX and machine code but perl did a faster job!!!, How cool is
> that?
>
>  Anyway, Have fun with it,
>             Jevon
>
>
> "Agustin Chernitsky" <agustinchernitsky@altavista.com> wrote in message
> news:8oj8od$b6ts4$1@ID-48235.news.cis.dfn.de...
> > Hi,
> >
> >     I created a scripts (an orders script) that uses an HTML file as a
> main
> > template. This HTML has a text (ie TEXTHERE) that indicates the program
to
> > insert other HTML code in that line. This program is made up of 3 parts:
> > main, an order summary and a contract info.
> >
> > These 3 parts use the same template. Now as I'm not an expert but I have
> > read Larry Wall's Learning Perl and Programming Perl, I was able to make
> > this program work. What I need now is to optimize the code and for that
I
> > need some experience. So, maybe someone here could help me.
> >
> > The code opens the template on all the 3 parts (I have 3 open commands
in
> > the program for the same file). I was thinking on placing all that on a
> sub
> > routine. What I don't is how to make the  program open the template and
> when
> > it detects the TEXTHERE, make it insert the new HTML code and after
that,
> > finish the template (imagine that the TEXTHERE is in the middle of the
> > HTML).
> >
> > Does anyone know how to handle this?
> >
> > Thanks!!!
> >
> > --
> > Agustín Chernitsky
> > InterEmpresa
> >
> > E-mail:       agustinchernitsky@altavista.com
> > ICQ:          20020826
> >
> >
>
>
>
>




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

Date: Wed, 30 Aug 2000 16:38:52 -0400
From: brian@smithrenaud.com (brian d foy)
Subject: Re: How to use a template with perl --- novice question
Message-Id: <brian-ya02408000R3008001638520001@news.panix.com>

In article <8ojqfe$assfo$1@ID-48235.news.cis.dfn.de>, "Agustin Chernitsky" <agustinchernitsky@altavista.com> posted:

> "brian d foy" <brian@smithrenaud.com> wrote in message
> news:brian-ya02408000R3008001620020001@news.panix.com...

> In article <39AD623B.8DBB13C6@shentel.net>, Albert Dewey
> <timewarp@shentel.net> posted:

> > First load the entire template in a variable,

> that could take up a lot of memory. you should consider if you really
> need to do that to accomplish the task (you probably don't).

> This is interesting. Brian, what other solution then?

read the file line-by-line or use a stream parser.  of course, 
this is a pain to code, so you could use one of the myriad
templating systems on CPAN instead.

-- 
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>


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

Date: Wed, 30 Aug 2000 21:15:25 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <c5uqqsgak9lrm4u24g1iuk8csk48hfc7ob@4ax.com>

Albert Dewey wrote:

>$webpage =~ s/TEXTHERE1/$newtext1/g;
>$webpage =~ s/TEXTHERE2/$newtext2/g;
>$webpage =~ s/TEXTHERE3/$newtext3/g;

Please don't do this in seubsequent steps. What if $newtext1 contains
"TEXTHERE2"? Do it all in one go.

	%replace = ( TEXTHERE1 => $newtext1, 
	  TEXTHERE2 => $newtext1, TEXTHERE3 => $newtext1 );
	$webpage =~ s/(TEXTHERE(?:1|2|3))/$replace{$1}/g;

And you definitely will want more recognizable variable markers; and a
way to escape them, in case you want that literal text to appear in your
output.

-- 
	Bart.


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

Date: Wed, 30 Aug 2000 14:36:13 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How to use a template with perl --- novice question
Message-Id: <MPG.14171e2ac945512298ad07@nntp.hpl.hp.com>

In article <c5uqqsgak9lrm4u24g1iuk8csk48hfc7ob@4ax.com> on Wed, 30 Aug 
2000 21:15:25 GMT, Bart Lateur <bart.lateur@skynet.be> says...
> Albert Dewey wrote:
> 
> >$webpage =~ s/TEXTHERE1/$newtext1/g;
> >$webpage =~ s/TEXTHERE2/$newtext2/g;
> >$webpage =~ s/TEXTHERE3/$newtext3/g;
> 
> Please don't do this in seubsequent steps. What if $newtext1 contains
> "TEXTHERE2"? Do it all in one go.
> 
> 	%replace = ( TEXTHERE1 => $newtext1, 
> 	  TEXTHERE2 => $newtext1, TEXTHERE3 => $newtext1 );
> 	$webpage =~ s/(TEXTHERE(?:1|2|3))/$replace{$1}/g;

  	$webpage =~ s/(TEXTHERE[123])/$replace{$1}/g;

Or you could factor the 'TEXTHERE' out of the capture and the hash keys.

And then you could index into an array of replacement strings.

 ...  :-)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 30 Aug 2000 18:13:58 GMT
From: abigail@foad.org (Abigail)
Subject: Re: interchanging variables
Message-Id: <slrn8qqjlk.8ac.abigail@alexandra.foad.org>

Eike Grote (Eike.Grote@consol.de) wrote on MMDLVI September MCMXCIII in
<URL:news:39AD1575.557A8CF8@consol.de>:
%% Hi,
%% 
%% Ankur Banerjee wrote:
%% > 
%% > HI,
%% > How do i interchange values of three variables $a,$b,$c WITHOUT  using
%% > this expression:-
%% > ($a,$b,$c)=($b,$c,$a);
%% > 
%% > I have thought of this:-
%% > $a=$a+$b+$c;
%% > $c=$a-($b+$c);
%% > $b=$a-($b+$c);
%% > $a=$a-($b+$c);
%% 
%% This will definitely cause problems if your variables contain
%% text, and may be troublesome if you use (floting point) numbers.

And it may cause problems with ordinary integers as well. Addition is
*not* a closed operation in the domain of computer integers.

%% Better is
%% 
%% { my $t=$a; $a=$b; $b=$c; $c=$t  }

There are of course a gazillion ways of doing this, but I'd like to know
why one would not want to use ($a, $b, $c) = ($b, $c, $a);?

    {local @_ = ($a, $c, $b);
     $a = pop; $b = pop; $c = pop;}

    {my @foo = ($a, $b, $c);
     ($a, $b, $c) = @foo [1, 2, 0];}

    {foreach ($a, $b, $c) {s/[-:]/"-" . ($& eq "-" ? "?" : "!")/eg;}
             ($a, $b, $c) = split /:/ => join ":" => $b, $c, $a;
     foreach ($a, $b, $c) {s/-(.)/$1 eq "?" ? "-" : ":"/eg;}}

    {my $la = length $a;
     my $lb = length $b;
     my $lc = length $c;
     my $tt = $b.$c.$a;
     $a = substr $tt => 0, $lb, "";
     $b = substr $tt => 0, $lc, "";
     $c = substr $tt => 0, $la, "";}


Abigail
-- 
($;,$_,$|,$\)=("\@\x7Fy~*kde~box*Zoxf*Bkiaox"," "x25,1,"\r"); 
{vec($_=>1+$"=>$^F<<$^F)=ord($/^substr$;=>$"=int rand 24=>1);              
 print&&select$,,$,,$,,$|/($|+tr/ //c);redo if y/ //>$^F**2};


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

Date: Wed, 30 Aug 2000 18:30:35 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: interchanging variables
Message-Id: <sqqkmbdsc5d163@corp.supernews.com>

Ankur Banerjee (ankban4@indiainfo.com) wrote:
: How do i interchange values of three variables $a,$b,$c WITHOUT  using
: this expression:-
: ($a,$b,$c)=($b,$c,$a);

What's with the overrestricted challenges lately?  Why avoid using a
perfectly wonderful language feature?

: I have thought of this:-
: $a=$a+$b+$c;
: $c=$a-($b+$c);
: $b=$a-($b+$c);
: $a=$a-($b+$c);

Only works for numbers, of course.  The most straightforward ways (other
than the one you exclude) all involve assignment through temporaries,
which are prevented from reducing to the simple version above only by your
fiat.

-- 
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "Every force evolves a form."
   |              - Shriekback


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

Date: Wed, 30 Aug 2000 18:50:02 GMT
From: Paul Eckert <peckert@epicrealm.com>
Subject: Re: interchanging variables
Message-Id: <39AD577E.8989D638@epicrealm.com>


Abigail wrote:
> pertinent stuff
> Abigail
> --
> ($;,$_,$|,$\)=("\@\x7Fy~*kde~box*Zoxf*Bkiaox"," "x25,1,"\r");
> {vec($_=>1+$"=>$^F<<$^F)=ord($/^substr$;=>$"=int rand 24=>1);
>  print&&select$,,$,,$,,$|/($|+tr/ //c);redo if y/ //>$^F**2};

I just tried running your signature above!  
I can't believe how cool this latest one is.  
I think I learn more from figuring out (if I can) how these work
and what they are doing than from anything else in this group!
Keep it up!

-- 
Paul Eckert
Sr. Software Engineer
Epicrealm Inc.
1651 N. Glenville Dr., Suite 212
Richardson, TX 75081
(972) 479-0135 x300
peckert@epicrealm.com


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

Date: Wed, 30 Aug 2000 13:21:58 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: interchanging variables
Message-Id: <MPG.14170cc19254315098ad04@nntp.hpl.hp.com>

In article <sacqqs49bplf5jhrf2r2sfkn41udve8om5@4ax.com> on Wed, 30 Aug 
2000 12:10:13 -0400, soybean <soybean_tofu@yahoo.com> says...
> On Wed, 30 Aug 2000 13:40:27 GMT, Ankur Banerjee
> <ankban4@indiainfo.com> wrote:
> 
> >Any other ways to do it ??
> 
> Hi
> 
> This works for me :)
> 
> @vars=('a'..'c');  # or any set or variable names
> 
> for (@vars) { # or any other way that you set your variables
>     $$_ = $x++;
> }

 ...

You won't get much support around here for any solution that 
gratuitously uses symbolic references.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Wed, 30 Aug 2000 21:11:03 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: interchanging variables
Message-Id: <k2tqqs0b4a7vlpsrpv2l03t6d43isn4n0k@4ax.com>

Ankur Banerjee wrote:

>How do i interchange values of three variables $a,$b,$c WITHOUT  using
>this expression:-
>($a,$b,$c)=($b,$c,$a);

Well, that is the Perl way. Any other way can be done in almost any
language.

This might work if all are integersor equal length strings:

	($a, $b, $c) = (4, 1, 2);
	# ($a, $b, $c) = ("baz", "foo", "bar");
	print "We have: $a $b $c\n";
	print "We want: $b $c $a\n";
	$b ^= $c;
	$c ^= $a;
	$a ^= $b;
	$a ^= $c;
	$b ^= $a;
	$c ^= $b;
	print "We got:  $a $b $c\n";

-- 
	Bart.


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 4180
**************************************


home help back first fref pref prev next nref lref last post