Using Perl to add hyperlink to html files

Hi ,

I have written a csh script which will output file named " myoutput.html " displayed below. I am tried to ftp this html file into a local web server so that it can be displayed to public sharing this server and i would want to add a hyperlink to " XX " , " YY " , " ZZ ". Can this be done using perl ? Can anybody show me an example of the code to perform this ?

XX 20 $50
YY 30 $60
ZZ 40 $70

It doesn't look like HTML to me.

I thought you would want to generate

<html>
    <head>
          <title>My webpage</title>
    <head>
    <body>
        <a href="some-url">XX</a> 20 $50<br>
        <a href="some-url">YY</a> 30 $60<br>
        <a href="some-url">ZZ</a> 40 $70<br> 
    </body>
</html>

Hi Porter,

Can i use perl to create your HTML code before i ftp ?
I would want to perform everything in my Solaris OS that is

1) generate the .html extension file using csh
2) create hyperlink in the html file ( using perl maybe or html ? )
3) ftp to the server

Certainly.

But Porter,

Can you show me the perl code using this example which is able to create the html and ftp to the server?
I am a novice in this area . Thanks

#!/usr/bin/perl

print "<html>\r\n";
print "\t<head>\r\n";
print "\t\t<title>My webpage</title>\r\n";
print "\t<head>\r\n";
print "\t<body>\r\n";
print "\t\t<a href=\"some-url\">XX</a> 20 \$50<br>\r\n";
print "\t\t<a href=\"some-url\">YY</a> 30 \$60<br>\r\n";
print "\t\t<a href=\"some-url\">ZZ</a> 40 \$70<br>\r\n";
print "\t</body>\r\n";
print "</html>\r\n";

Hi Porter,

How about the ftp part ? How can i ftp this whole portion over?
I tried to copy everything into a file named webfile by using print "<html>\r\n"; > webfile but it's showing syntax error.

I personally wouldn't do that in Perl.

a. edit $HOME/.netrc to add username/password

b. use the following shell script

ftp server <<EOF
put x.html /some/directory/x.html
EOF

Can i do the appending of lines to file name " webfile " so as to ease the ftp portion by shell ? I try to perform the below but it's showing some error . Can you help ?

#!/usr/bin/perl

print "<html>\r\n" > webfile ;
print "\t<head>\r\n" >> webfile ;
print "\t\t<title>My webpage</title>\r\n" >> webfile;
print "\t<head>\r\n" >> webfile ;
print "\t<body>\r\n" >> webfile ;
print "\t\t<a href=\"some-url\">XX</a> 20 \$50<br>\r\n" >> webfile;
print "\t\t<a href=\"some-url\">YY</a> 30 \$60<br>\r\n" >> webfile;
print "\t\t<a href=\"some-url\">ZZ</a> 40 \$70<br>\r\n" >> webfile;
print "\t</body>\r\n" >> webfile;
print "</html>\r\n" >> webfile;

That is because you are trying to write shell code in a Perl script.

Assuming you put your Perl in "myperl.pl" then do the following from shell...

./myperl.pl >webpage.html
#!/usr/bin/perl
use Net::FTP;
open(OUT,">>fileout.html");
$XX="XX";
$XXval="20";
$var = <<"EOF";
<html>
<head>
    <title>My webpage</title>
    <head>
    <body>
    a href="some-url"> $XXval </a> $XX <br>
EOF
print OUT $var;
close(OUT);

$ftp = Net::FTP-> new
(
 "ftp.server.com",
 Timeout=>30
 
) or die "Cannot connect: $!"
$username="me";
$pass="password";
$ftp->login($username,$pass) or die "Could not log on: $!";
$ftp->cwd('/path');
$ftp->put("fileout.html");
$ftp->quit;

just an example. you do the rest. If you want to use Perl, learn it first.

Hi GhostDog,

Thanks for your code.
I roughly know how your perl code works now.
But just one more question.

My shell script created a file named " shelloutput "

where " shelloutput " is the below:
XX 20 $50
YY 30 $60
ZZ 40 $70

How can i pass each term in this file as a variable to the perl code?
In your earlier code, you have assumed 20 as the term for your $XXval variable but in actual fact we do not know until we actually look at the shelloutput file ourself. What we know is that the ouput will be in the form of a 3 x 3 matrix where each term is seperated by spaces and the $XXval will always be the 2nd field if "XX" is the first field

you just have to open the file and read the contents. An example reference. pls get a Perl book and start learning.

Hi GhostDog,

Thanks for the example. I think i have got it!

#!/usr/bin/perl
use Net::FTP;
$[ = 1;                 # set array base to 1
open( FILE, "< filename" ) or die "Can't open $filename : $!";

    while( <FILE> ) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq 'XX') {
    $TESTER = $Fld[1];
    $ID = $Fld[2];
    $QTY = $Fld[3];
    }
    close FILE;
}

open(OUT,">>fileout.html");
$var = <<"EOF";
<html>
<head>
    <title>My webpage</title>
    <head>
    <body>
    <a href="some-url"> $TESTER </a> $ID  $QTY <br>
</head>
</html>

EOF
print OUT $var;
close(OUT);

However, when i introduce the path reference " $filename = "/scripts/myperl/filename" ", there's some error , can you give some guidance on that ?

#!/usr/bin/perl
use Net::FTP;
$[ = 1;                 # set array base to 1
$filename = "/scripts/myperl/filename"
open( FILE, "< $filename" ) or die "Can't open $filename : $!";

    while( <FILE> ) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq 'XX') {
    $TESTER = $Fld[1];
    $ID = $Fld[2];
    $QTY = $Fld[3];
    }
    close FILE;
}

open(OUT,">>fileout.html");
$var = <<"EOF";
<html>
<head>
    <title>My webpage</title>
    <head>
    <body>
    <a href="some-url"> $TESTER </a> $ID  $QTY <br>
</head>
</html>

EOF
print OUT $var;
close(OUT);

well, don't expect anyone to guess the error, especially me. Post your errors if any, if not, try to include the terminating semi colon after the line.

Hi GhostDog,

Thanks, i miss out the semi colon! You are cool!!

By the way, i am trying to align each text displayed on the browser so that each column starts from the same vertical line. Do you know which html tag does the trick ?

not a very good solution, but try replacing this:

<body>

with:

<body style="text-indent=8">

Hi Yogesh Sawant,

Thanks for your help!