You are not logged in.

#1 2009-09-08 09:03:34

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] Anyone know something about perl?

Hey guys.  I got a perl script off the net that can encode html entities from the command line.  The syntax is:

echo 'some & "HTML" <entities>' | html-encode
some & "HTML" <entities>

I'd like the script ordering to be different so that command comes first, followed by the string to be converted:

html-encode some & "HTML" <entities>

I've read a bit about perl but I realize I am only just beginning.  If anyone knows enough about perl to help me out, I'd really appreciate it.  Here's the script:

#!/usr/bin/perl

# ----------------------------------------------------------------------
# $Id$
# This simple script takes data and HTML encodes it.
# If passed a -e, it will use entities instead of symbolic names
# ----------------------------------------------------------------------

use strict;
use vars qw($opt_h $opt_e $encoder);

use Getopt::Std qw(getopts);
use HTML::Entities;

getopts('eh');

if (defined $opt_h) {
    require File::Basename;
    $0 = File::Basename::basename($0);
    print <<HELP;
$0 -- Encode HTML files

Usage:

    $0 [-e] FILE [, FILE[, FILE]]

HTML encodes the contents of FILE (or stdin, if no files are
given) to stdout.  If -e is passed, $0 does numeric encoding,
i.e., < and > instead of < and >.

HELP
    exit 0;
}

if (defined $opt_e) {
    $encoder = \&HTML::Entities::encode_entities_numeric;
}
else {
    $encoder = \&HTML::Entities::encode_entities;
}

# If passed filenames on the command line, iterate through those,
# emitting to STDOUT; otherwise, read STDIN.
if (@ARGV) {
    require IO::File;
    for my $file (@ARGV) {
        my $fh = IO::File->new($file);

        unless ($fh) {
            warn "Can't read $file: $!\n";
            next;
        }

        while (<$fh>) {
            print $encoder->($_);
        }

        unless (close $fh) {
            warn "Error closing $file: $!\n";
        }
    }
}
else {
    while (<>) {
        print $encoder->($_);
    }
}

exit 0;

Last edited by Gen2ly (2009-09-10 15:49:39)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2009-09-08 09:17:55

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [Solved] Anyone know something about perl?

If I've understood your question correctly, you seem to be confused about what's going on when you pipe the command.

The command isn't "coming last" in the order of arguments. The echo command is separate and is piping the output to html-encode. The argument order has nothing to do with it.

Here's a modified version which accepts command-line arguments as strings to convert instead of file names. This will not work with files as the previous one but will do what you want.

#!/usr/bin/perl

# ----------------------------------------------------------------------
# $Id$
# This simple script takes data and HTML encodes it.
# If passed a -e, it will use entities instead of symbolic names
# ----------------------------------------------------------------------

use strict;
use vars qw($opt_h $opt_e $encoder);

use Getopt::Std qw(getopts);
use HTML::Entities;

getopts('eh');

if (defined $opt_h) {
    require File::Basename;
    $0 = File::Basename::basename($0);
    print <<HELP;
$0 -- Encode HTML files

Usage:

    $0 [-e] FILE [, FILE[, FILE]]

HTML encodes the contents of FILE (or stdin, if no files are
given) to stdout.  If -e is passed, $0 does numeric encoding,
i.e., < and > instead of < and >.

HELP
    exit 0;
}

if (defined $opt_e) {
    $encoder = \&HTML::Entities::encode_entities_numeric;
}
else {
    $encoder = \&HTML::Entities::encode_entities;
}

# If passed filenames on the command line, iterate through those,
# emitting to STDOUT; otherwise, read STDIN.
if (@ARGV) {
    while (my $arg = shift) {
      print $encoder->($arg);
    }
}
else {
    while (<>) {
        print $encoder->($_);
    }
}

exit 0;

Note that both this version and the original will accept strings if you run the script without any input or arguments.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2009-09-08 10:49:19

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Anyone know something about perl?

Hey that's great Xine, more of what I was looking for.  Is it possible to include in the script a way to add a string without the quotes?

html-encode 'some & "HTML" <entities>'
html-encode some & "HTML" <entities>

Or is that not possible?  Also could you have it add a newline on output because my PS1 is getting appended to the end of encoded line.  It be great... if it isn't too much to ask. big_smile


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#4 2009-09-08 11:58:03

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [Solved] Anyone know something about perl?

You need to quote the arguments if you pass them on the command line. How else would bash (or whatever shell you're using) know that it's an argument and not a command to the shell (e.g. "&" would background the process). There seems to be some confusion again about the difference between shell commands and program arguments.

You can just run the script without passing it anything (html-encode) and then feed in what you want without quoting it. That works because once the script is running, all of the input goes directly to it instead of through the shell.

Here's an updated version which appends a newline:

#!/usr/bin/perl

# ----------------------------------------------------------------------
# $Id$
# This simple script takes data and HTML encodes it.
# If passed a -e, it will use entities instead of symbolic names
# ----------------------------------------------------------------------

use strict;
use vars qw($opt_h $opt_e $encoder);

use Getopt::Std qw(getopts);
use HTML::Entities;

getopts('eh');

if (defined $opt_h) {
    require File::Basename;
    $0 = File::Basename::basename($0);
    print <<HELP;
$0 -- Encode HTML files

Usage:

    $0 [-e] FILE [, FILE[, FILE]]

HTML encodes the contents of FILE (or stdin, if no files are
given) to stdout.  If -e is passed, $0 does numeric encoding,
i.e., < and > instead of < and >.

HELP
    exit 0;
}

if (defined $opt_e) {
    $encoder = \&HTML::Entities::encode_entities_numeric;
}
else {
    $encoder = \&HTML::Entities::encode_entities;
}

# If passed filenames on the command line, iterate through those,
# emitting to STDOUT; otherwise, read STDIN.
if (@ARGV) {
    while (my $arg = shift) {
      print $encoder->($arg);
    }
}
else {
    while (<>) {
        print $encoder->($_);
    }
}
print "\n";
exit 0;

My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#5 2009-09-09 04:26:56

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Anyone know something about perl?

Thanks xine the newline is a big plus.  For the quotes, because I've begun in bash was thinking that perl might be able to capture all output following a command similar to what I use "$@" for in my bash scripts.  Typically I just copy and paste to the terminal so I was hoping to save the extra key steps to typing quotes, or maybe I'm just lazy.  Who knows.  smile  As it is, this will do.  Thanks for the help.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#6 2009-09-09 08:37:33

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: [Solved] Anyone know something about perl?

In case you are pasting some text to your shell command line and  then copy the result from the terminal to paste it somewhere else, consider binding something like this to a key:

xclip -o | html-encode | xclip -i

This runs the html-encode script on the contents of your clipboard without you having to enter the command in a terminal. Just copy some text, invoke this command, and paste the encoded text somewhere else.

Note that you should use the original script for this, otherwise you'll end up with with superfluous newlines in case your clipboard contains multiple lines of text.

Last edited by hbekel (2009-09-09 08:37:57)

Offline

#7 2009-09-10 13:43:27

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [Solved] Anyone know something about perl?

Gen2ly wrote:

Thanks xine the newline is a big plus.  For the quotes, because I've begun in bash was thinking that perl might be able to capture all output following a command similar to what I use "$@" for in my bash scripts.  Typically I just copy and paste to the terminal so I was hoping to save the extra key steps to typing quotes, or maybe I'm just lazy.  Who knows.  :)  As it is, this will do.  Thanks for the help.

There's nothing wrong with being lazy :)

But I'm a little confused by your second sentence.  You still seem to be laboring under a mistaken idea, but I'm not exactly sure what it is.  Interpolating @ARGV in a string will get you the equivalent of "$@", but you still must quote arguments given on the command line, because special characters (@#$\&<>(){}!, and in some cases ~ and *) are interpreted by the shell that invokes your script, and are not passed in any way to the script.  The usage you asked about, i.e.

html-encode some & "HTML" <entities>

will fail without respect to the particular script because the shell will try to background "html-encode some" and run another command named HTML, drawing its input from a nonexistent "entities" file and sending its output to... well, there's no filename after the >, so that's a syntax error.  Do you see why?

If you didn't realize this, or didn't understand what Xyne said, the original Perl script takes data from standard input, which is not interpreted by the shell.  There's no need to use echo; just type the name of the script and hit Enter.  Copy and paste all the &'s and <>'s you want; the shell doesn't care, because it doesn't capture stdin.  When you're done, press Ctrl-D (on a blank line) to signify that your input has come to an end.

Offline

#8 2009-09-10 15:49:19

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] Anyone know something about perl?

Yeah, I got it Trent.  Understand now that bash reads the array before perl would interpret it and would interpret and following chracter/words as commands.  Got everything running now, thanks.  Just a case of me trying to get too lazy and not a night of enough coffee.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB