Mailing List

Flat
Substitution expression
User: Spizzat2
Date: 9/2/2010 10:31 am
Views: 1357
Rating: 0
I'm working with HTML::Mason, which allows you to build web pages in a fashion very similar to PHP in that it lets you intersperse Perl code into the HTML page as it's being built by wrapping expressions in Mason tags ( which look like this <% %> ). Obviously, Perl could do this alone, but Mason provides other features.
Anyway, the problem I'm having is that I'd like to escape quotes from a string and spit it out onto the page. I can do it outside of the expression tags using a simple substitution, but it requires two statements to do it.
my $str = $ARGS{'search'}; #copy to local variable
$str=~s/"/\&quot;/g; #Make the substitution
Then I'm able to use the expression <% $str %> to get the value I need. Unfortunately, Mason doesn't allow multiple statements between the expression tags, so I'm wondering if there's some way to reduce this all to an inline expression. I know that in Javascript, I would just do str.replace(); PHP would use str_replace(), too. Is there a simple expression in Perl that would allow me to do this all inline?
Thanks,
Karl
Re: Substitution expression
User: mcholste
Date: 9/2/2010 11:05 am
Views: 164
Rating: 0
What about all the other unsafe chars?  I'd go canonical:

use URI::Encode qw(uri_encode);

<% uri_encode($ARGS{'search'}) %>

On Thu, Sep 2, 2010 at 10:31 AM,   wrote:
> Spizzat2 wrote:
>
> I'm working with HTML::Mason, which allows you to build web pages in a
> fashion very similar to PHP in that it lets you intersperse Perl code into
> the HTML page as it's being built by wrapping expressions in Mason tags (
> which look like this <% %> ). Obviously, Perl could do this alone, but Mason
> provides other features.
> Anyway, the problem I'm having is that I'd like to escape quotes from a
> string and spit it out onto the page. I can do it outside of the expression
> tags using a simple substitution, but it requires two statements to do it.
>>
>> my $str = $ARGS{'search'}; #copy to local variable
>> $str=~s/"/\&quot;/g; #Make the substitution
>
> Then I'm able to use the expression <% $str %> to get the value I need.
> Unfortunately, Mason doesn't allow multiple statements between the
> expression tags, so I'm wondering if there's some way to reduce this all to
> an inline expression. I know that in Javascript, I would just do
> str.replace(); PHP would use str_replace(), too. Is there a simple
> expression in Perl that would allow me to do this all inline?
> Thanks,
> Karl
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>
Re: Substitution expression
User: esayward
Date: 9/2/2010 11:18 am
Views: 0
Rating: 0
I don't understand.. what exactly do you need to web encode the quotes for? is this for DB or HTML display reasons? -E
----- Original Message -----
Sent: Thursday, September 02, 2010 11:05 AM
Subject: [MadTalk] Re: Substitution expression

mcholste wrote:

What about all the other unsafe chars?  I'd go canonical:

use URI::Encode qw(uri_encode);

<% uri_encode($ARGS{'search'}) %>

On Thu, Sep 2, 2010 at 10:31 AM,   wrote:
> Spizzat2 wrote:
>
> I'm working with HTML::Mason, which allows you to build web pages in a
> fashion very similar to PHP in that it lets you intersperse Perl code into
> the HTML page as it's being built by wrapping expressions in Mason tags (
> which look like this <% %> ). Obviously, Perl could do this alone, but Mason
> provides other features.
> Anyway, the problem I'm having is that I'd like to escape quotes from a
> string and spit it out onto the page. I can do it outside of the expression
> tags using a simple substitution, but it requires two statements to do it.
>>
>> my $str = $ARGS{'search'}; #copy to local variable
>> $str=~s/"/\&quot;/g; #Make the substitution
>
> Then I'm able to use the expression <% $str %> to get the value I need.
> Unfortunately, Mason doesn't allow multiple statements between the
> expression tags, so I'm wondering if there's some way to reduce this all to
> an inline expression. I know that in Javascript, I would just do
> str.replace(); PHP would use str_replace(), too. Is there a simple
> expression in Perl that would allow me to do this all inline?
> Thanks,
> Karl
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
>

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: Substitution expression
User: preaction
Date: 9/2/2010 11:32 am
Views: 171
Rating: 0
Why are you escaping the quotes? 

Is it for inside an HTML tag? use HTML::Entities. 
<% encode_entities( $string ) %>

For a URL? URI::Escape. 
<% uri_encode( $string ) %>

For inside Perl code? quotemeta().
<% quotemeta( $string ) %>
Re: Substitution expression
User: Spizzat2
Date: 9/2/2010 11:47 am
Views: 182
Rating: 0

It's in an HTML attribute. Those functions should provide me what I need. I already had an alternative solution, though. This was more to see if it could be done theoretically.

On Sep 2, 2010 11:46 AM, <doug@plainblack.com> wrote:

preaction wrote:

Why are you escaping the quotes? 

Is it for inside an HTML tag? use HTML::Entities. 
<% encode_entities( $string ) %>

For a URL? URI::Escape. 
<% uri_encode( $string ) %>

For inside Perl code? quotemeta().
<% quotemeta( $string ) %>



View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Substitution expression
User: Spizzat2
Date: 9/2/2010 11:55 am
Views: 0
Rating: 0
Thanks, Andy. That's the theoretical solution I was looking for.
Thanks to everyone else for pointing me to some good packages that solve the problem.

On Thu, Sep 2, 2010 at 11:47 AM, <madtalk@madmongers.org> wrote:

Spizzat2 wrote:

It's in an HTML attribute. Those functions should provide me what I need. I already had an alternative solution, though. This was more to see if it could be done theoretically.

On Sep 2, 2010 11:46 AM, <doug@plainblack.com> wrote:

preaction wrote:

Why are you escaping the quotes? 

Is it for inside an HTML tag? use HTML::Entities. 
<% encode_entities( $string ) %>

For a URL? URI::Escape. 
<% uri_encode( $string ) %>

For inside Perl code? quotemeta().
<% quotemeta( $string ) %>



View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

Re: Substitution expression
User: miner
Date: 9/2/2010 12:37 pm
Views: 188
Rating: 0
And, of course, if it's something to do with a database, $dbh->quote( $string ).

jon

On 9/2/10 11:32 AM, doug@plainblack.com wrote:

preaction wrote:

Why are you escaping the quotes? 

Is it for inside an HTML tag? use HTML::Entities. 
<% encode_entities( $string ) %>

For a URL? URI::Escape. 
<% uri_encode( $string ) %>

For inside Perl code? quotemeta().
<% quotemeta( $string ) %>

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

-- 
.Jonathan J. Miner----------------------------------------------------.
|  jon@jjminer.org  |      photos - http://photos.jjminer.org/        |
|                   | R.A.W. #1629 - http://www.reggaeambassadors.org |
|                   | LOCS Webmaster - http://www.locs-buffett.org    |
|  jabber/gchat: camrycurbhopper@gmail.com      AIM: camrycurbhopper  |
`---------------------------------------------------------------------'

"We don't have a town drunk...   We all take turns!"
    -- James Slater, "Key West Address"
Re: Substitution expression
User: esayward
Date: 9/2/2010 1:05 pm
Views: 0
Rating: 0
why not then do it like this..
 
my $insert_sth = $dbh->prepare("INSERT INTO `table` (`value1`, `value2`) VALUES ( ?, ?);") or die $DBI::errstr;
$insert_sth->execute($value1, $value2) or die $DBI::errstr;
$insert_sth->finish();
 
I'm assuming you are trying to avoid Injection Issues..??
 
----- Original Message -----
Sent: Thursday, September 02, 2010 12:37 PM
Subject: [MadTalk] Re: Substitution expression

miner wrote:

And, of course, if it's something to do with a database, $dbh->quote( $string ).

jon

On 9/2/10 11:32 AM, doug@plainblack.com wrote:

preaction wrote:

Why are you escaping the quotes? 

Is it for inside an HTML tag? use HTML::Entities. 
<% encode_entities( $string ) %>

For a URL? URI::Escape. 
<% uri_encode( $string ) %>

For inside Perl code? quotemeta().
<% quotemeta( $string ) %>

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

-- 
.Jonathan J. Miner----------------------------------------------------.
|  jon@jjminer.org  |      photos - http://photos.jjminer.org/        |
|                   | R.A.W. #1629 - http://www.reggaeambassadors.org |
|                   | LOCS Webmaster - http://www.locs-buffett.org    |
|  jabber/gchat: camrycurbhopper@gmail.com      AIM: camrycurbhopper  |
`---------------------------------------------------------------------'

"We don't have a town drunk...   We all take turns!"
    -- James Slater, "Key West Address"

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: Substitution expression
User: afbach
Date: 9/2/2010 11:46 am
Views: 0
Rating: 0
On Thu, Sep 2, 2010 at 10:31 AM,   wrote:
>> my $str = $ARGS{'search'}; #copy to local variable
>> $str=~s/"/\&quot;/g; #Make the substitution

I think those are all good points - quote is only one of your worries,
rigth? But just to take a stab at your orig. question:
(my $str = $ARGS{'search'} ) =~ s/"/\&quot;/g;

I imagine you could skip the local str too and operate/use on %ARGS -
I doubt it'd break anything 'upstream', though it is a bit ugly for a
lot of use.

--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: Substitution expression
User: zjt
Date: 9/2/2010 12:12 pm
Views: 0
Rating: 0
On 09/02/2010 10:31 AM, madtalk@madmongers.org wrote:
> Unfortunately, Mason doesn't allow multiple statements between the
> expression tags

maybe not *those* tags, but you can use other tags

from:
http://search.cpan.org/~drolsky/HTML-Mason-1.45/lib/HTML/Mason/Devel.pod

% lines

Most useful for conditional and loop structures - if, while, foreach, ,
etc. - as well as side-effect commands like assignments. To improve
readability, always put a space after the '%'. Examples:

o Conditional code

    % my $ua = $r->header_in('User-Agent');
    % if ($ua =~ /msie/i) {
    Welcome, Internet Explorer users
    ...
    % } elsif ($ua =~ /mozilla/i) {
    Welcome, Netscape users
    ...
    % }

o HTML list formed from array

   
    % foreach $item (@list) {
    <% $item %>
    % }
   

o HTML list formed from hash

   
    % while (my ($key,$value) = each(%ENV)) {
   
    <% $key %>: <% $value %>
   
    % }
   

o HTML table formed from list of hashes

   
    % foreach my $h (@loh) {
   
    <% $h->{foo} %>
    <% $h->{bar} %>
    <% $h->{baz} %>
   
    % }
   

<% xxx %>

Most useful for printing out variables, as well as more complex
expressions. To improve readability, always separate the tag and
expression with spaces. Examples:

  Dear <% $name %>: We will come to your house at <% $address %> in the
  fair city of <% $city %> to deliver your $<% $amount %> dollar prize!

  The answer is <% ($y+8) % 2 %>.

  You are <% $age < 18 ? 'not' : '' %> permitted to enter this site.

<%perl> xxx

Useful for Perl blocks of more than a few lines.

Re: Substitution expression
User: Spizzat2
Date: 9/2/2010 12:28 pm
Views: 0
Rating: 0

Right, and I was hoping to get it all done in one expression like I can in many other languages. My initial solution was to do it in multiple lines, but Andy's code and the packages gave me a way to do it in one expression, so I could use the <% %> tags.

On Sep 2, 2010 12:27 PM, <jesse.thompson@doit.wisc.edu> wrote:

zjt wrote:



On 09/02/2010 10:31 AM, madtalk@madmongers.org wrote:
> Unfortunately, Mason doesn't allow multipl...

maybe not *those* tags, but you can use other tags

from:
http://search.cpan.org/~drolsky/HTML-Mason-1.45/lib/HTML/Mason/Devel.pod

% lines

Most useful for conditional and loop structures - if, while, foreach, ,
etc. - as well as side-effect commands like assignments. To improve
readability, always put a space after the '%'. Examples:

o Conditional code

    % my $ua = $r->header_in('User-Agent');
    % if ($ua =~ /msie/i) {
    Welcome, Internet Explorer users
    ...
    % } elsif ($ua =~ /mozilla/i) {
    Welcome, Netscape users
    ...
    % }

o HTML list formed from array

   
    % foreach $item (@list) {
    <% $item %>
    % }
   

o HTML list formed from hash

   
    % while (my ($key,$value) = each(%ENV)) {
   
    <% $key %>: <% $value %>
   
    % }
   

o HTML table formed from list of hashes

   
    % foreach my $h (@loh) {
   
    <% $h->{foo} %>
    <% $h->{bar} %>
    <% $h->{baz} %>
   
    % }
   

<% xxx %>

Most useful for printing out variables, as well as more complex
expressions. To improve readability, always separate the tag and
expression with spaces. Examples:

  Dear <% $name %>: We will come to your house at <% $address %> in the
  fair city of <% $city %> to deliver your $<% $amount %> dollar prize!

  The answer is <% ($y+8) % 2 %>.

  You are <% $age < 18 ? 'not' : '' %> permitted to enter this site.

<%perl> xxx

Useful for Perl blocks of more than a few lines.

View Online



Madison Area Perl Mongers - MadMongers
http://www.madmongers.org

PreviousNext
Madison Area Perl Mongers