tags:

views:

210

answers:

4

This is the input text:

<title>Company Selection</title>
<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />

and I want to substitute the CSS reference with two lines:

<%@ include file="../common/cmufx.jsp" %>
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbpSA_css.jsp" />

Here's the Perl statement:

while(<>){
    s{<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />}{
--><%@ include file="../common/cmufx.jsp" %> 
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbpSA_css.jsp" />;};
print;
}

And in output I get:

<title>Company Selection</title>

-->
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbpSA_css.jsp" />
  • If I remove the '%>' element at the end of the first substituting line, I get the line to be printed.
  • If the substituting line ends with '% >', it gets printed.
  • If I escape the %, the line does not get printed


The whole source of my script:

#!/usr/bin/perl

use strict;
use warnings;


while(<>){
    s{<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />}{
--><%@ include file="../common/cmufx.jsp" %>
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbpSA_css.jsp" />;};

    s{<link rel="stylesheet" type="text/css" href="../css/style_hbp_css.jsp" />}{
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbp_css.jsp" />};

    s{<%@ include file="../common/cmufx.jsp" %>}{};

    s{../img/banner.jpg}{<%=basePath%>/<fmt:message key="application.version"/>/img/banner.jpg};

    print;
}
+1  A: 

It just dawned on me, you are viewing the results through a web server aren't you? The <% %> stuff should be replaced by a template program or server side includes. You will never see that text. Most likely the file ../common/cmufx.jsp does not exist (e.g. the path is wrong) or it contains nothing. Take a look at what the program spits out on the commandline. If it is what you are expecting then the problem is somewhere else (e.g. the path to cmufx.jsp or your ssi not being setup right).

Your data has a space where your regex has a /:

data                                               V
<link rel="stylesheet" type="text/css" href="../css style_hbpSA_css.jsp" />
regex                                              V
<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />

Also, you are using . in your regex without escaping them. The . character in a regex matches any character other than a newline (unless the /s option is used). Your regex should probably look like

s{<link rel="stylesheet" type="text/css" href="\.\./css style_hbpSA_css\.jsp" />}{}

or

s{<link rel="stylesheet" type="text/css" href="[.][.]/css style_hbpSA_css[.]jsp" />}{}

depending on which looks less ugly to you. The first escapes the special meaning of ., and the second creates a character class of one character, the ..

You say you are still having problems, here is a full script that does the right thing, can you tell us where your code diverges from this:

#!/usr/bin/perl

use strict;
use warnings;

while(<DATA>){
s{<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />}{
<%@ include file="../common/cmufx.jsp" %> 
<link rel="stylesheet" type="text/css" href="<%=basePath%>/<fmt:message key="application.version"/>/css/style_hbpSA_css.jsp" />;};
print;
}

__DATA__
<title>Company Selection</title>
<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />
Chas. Owens
thanks for your answer, but the data is correct and it was me who inserted the typo while pasting it here. :(I have escaped the '.', but no success.
I see no difference at all between your script (which _is working fine_) and mine (that I published in the body of the question).The *only* difference is that I am reading from a separate file through calling 'script.pl FILE_NAME'.
I think slim might be right then. What OS are you using? Does the line endings in the file match the line endings of you OS?
Chas. Owens
The problem had another source: after this substitution, I was also applying another one: s{<%@ include file="../common/cmufx.jsp" %>}{};That's why my script was not working while yours was!I feel very stupid, but you put me on the right track.Thank you a lot.
No problem, I always find it is helpful to step outside of the problem code and replicate the problem. If I can make a toy version of the code work that generally tells me what is wrong with the real code.
Chas. Owens
A: 

Firstly, I strongly recommend against using regular expressions to manipulate HTML. Use something like HTML::Tree.

Nonetheless, when I tried your code, it worked. The only thing I changed was what I assume was a typo: your input included:

href="../css style_hbpSA_css.jsp"

Your pattern was:

href="../css/style_hbpSA_css.jsp"

I assume you wanted both to be the same (although it would be even better to have '..' in the pattern).

One thing I would check for is weird (that is, Windows) line endings in your input file.

slim
yes it was a typo: my data is correct.and yes I am on windows..
You may therefore be writing both lines, but with UNIX newlines, meaning the second line overwrites the first.
slim
good idea, but it's not true because the '-->' string get still printed (check the updated body of the question)
A: 

The answer is that after that substitution, I was ALSO requesting

s{<%@ include file="../common/cmufx.jsp" %>}{};

therefore the line got deleted.

sorry for trashing your time and I do thank you for all the answers.

A: 

It's because you have a new line after {

s{<link rel="stylesheet" type="text/css" href="../css/style_hbpSA_css.jsp" />}{ #your new line is here
<%@ include file="../common/cmufx.jsp" %>
Cadoo