[25018] in Perl-Users-Digest
Perl-Users Digest, Issue: 7268 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 19 00:06:55 2004
Date: Mon, 18 Oct 2004 21:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 18 Oct 2004 Volume: 10 Number: 7268
Today's topics:
Re: Calling a perl script from an html doc <jurgenex@hotmail.com>
Re: date format <abigail@abigail.nl>
Editable webpage code - security loopholes? (Duke of Hazard)
Re: Editable webpage code - security loopholes? <postmaster@castleamber.com>
Re: Editable webpage code - security loopholes? <see@sig.invalid>
Re: Editable webpage code - security loopholes? <postmaster@castleamber.com>
Re: How do I print http 404 not found header? <noreply@gunnar.cc>
Re: How do I print http 404 not found header? lesley_b_linux@yahoo.co.yuk
Re: How do I print http 404 not found header? <flavell@ph.gla.ac.uk>
Re: How do I print http 404 not found header? <flavell@ph.gla.ac.uk>
Re: How do I print http 404 not found header? <abigail@abigail.nl>
Re: How do I print http 404 not found header? <jurgenex@hotmail.com>
Re: How do I print http 404 not found header? <jurgenex@hotmail.com>
Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multi <abigail@abigail.nl>
Re: options to shrink-wrap a perl script (dan baker)
Re: options to shrink-wrap a perl script <abigail@abigail.nl>
Re: options to shrink-wrap a perl script <jkeen_via_google@yahoo.com>
Re: regex to clean path <uguttman@athenahealth.com>
Re: regex to clean path <abigail@abigail.nl>
Re: Top posting <tadmc@augustmail.com>
Re: Top posting <Jon.Ericson@jpl.nasa.gov>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 19 Oct 2004 01:20:02 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Calling a perl script from an html doc
Message-Id: <6ZZcd.2595$EP4.2479@trnddc06>
lesley_b_linux@yahoo.co.yuk wrote:
> While we're on the subject of CGI, I've always understood it to stand
> for Common Gateway Interface. From my experience the end result is
> that some perl scripts are executed.
That is a very limited point of view. Or a very limited experience.
> Has there ever been any other
> language used with CGI or was CGI specifically designed to work with
> Perl and nothing else?
OHMYGOD. You are kidding, aren't you?
jue
------------------------------
Date: 19 Oct 2004 00:35:16 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: date format
Message-Id: <slrncn8oa3.img.abigail@alexandra.abigail.nl>
Yasaswi Pulavarti (yasaswi@encfor.com) wrote on MMMMLXVI September
MCMXCIII in <URL:news:aa862233.0410180743.1fdf5cca@posting.google.com>:
"" I have a text file I need to load into MySQL database. In the text
"" file the date is shown as mm/dd/yyyy format. However I need to enter
"" the date in MySQL native yyyy-mm-dd format. How can I change
"" formatting without using substring function?
I got to ask this: why the requirement to not use substr? For obfuscation?
May I then suggest (still needs some work):
$str=do{local$"="-";"@{[@{[unpack+q=A2AA2AA4==>$str]}[split//=>0622]]}"};
Abigail
--
perl -we 'eval {die ["Just another Perl Hacker\n"]}; print ${${@}}[$#{@{${@}}}]'
------------------------------
Date: 18 Oct 2004 17:41:00 -0700
From: squash@peoriadesignweb.com (Duke of Hazard)
Subject: Editable webpage code - security loopholes?
Message-Id: <2d21b838.0410181641.16bb543@posting.google.com>
I have developed a really basic script to make my web pages editable.
I am tired of having to hop around several applications and menus just
to fix one spelling error on a webpage! Also this empowers my clients
to update their own sites as far as text goes.
Basically it reads in the webpage file you want to edit and displays
its content in a textarea box. If the password matches, the script
overwrites the webpage file with the new text you typed in the
textarea.
It works, but what security checks do I need to perform on it? I
realize I need to sanitize the password field, how about the textarea
field?
Thanks!
Here's a snipet of the code below:
==================================
$file_path=$incoming{'file_path'};
$password=$incoming{'password'};
@textarea=$incoming{'textarea'};
# overwrite file if password matches with contents of @message
if ($password eq "12345"){
open(F, ">$file_path") ;
flock(F, 2); # lock file
print F "@textarea";
close(F);
}
# display web page to allow user to edit web page
print "Content-type: text/html\n\n";
print"<form action=/cgi-bin/edit_webpage.pl method=POST>
<textarea name=message cols=75 rows=25>";
open(F, "$file_path") ;
print <F>;
print"</textarea>";
close(F);
print' <input type="password" name="password"> ';
print' <input type="submit" value="Edit"> ';
------------------------------
Date: 19 Oct 2004 01:19:06 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Editable webpage code - security loopholes?
Message-Id: <Xns9586CEB06A90Acastleamber@130.133.1.4>
Duke of Hazard wrote:
> It works, but what security checks do I need to perform on it?
How can we tell if you show just a snippet.
But the snippet is enough to guess that you:
[1] don't use strict, nor use warnings;
[2] don't use taint mode
[3] don't check return values (open, flock, etc)
[4] don't check file_path
[5] probably don't use CGI.pm
> I
> realize I need to sanitize the password field, how about the textarea
> field?
Just a quick tip for your password. Never store it readable. Get the
password, calculate an MD5 hash, and compare it with the MD5 hash of the
actual password. Of course you have calculated the latter with a separate
script, so the actual password is nowhere stored in readable form.
Also read about here documents, it will simplify your code.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Mon, 18 Oct 2004 21:51:04 -0400
From: Bob Walton <see@sig.invalid>
Subject: Re: Editable webpage code - security loopholes?
Message-Id: <417470f1$1_5@127.0.0.1>
Duke of Hazard wrote:
> I have developed a really basic script to make my web pages editable.
> I am tired of having to hop around several applications and menus just
> to fix one spelling error on a webpage! Also this empowers my clients
> to update their own sites as far as text goes.
>
> Basically it reads in the webpage file you want to edit and displays
> its content in a textarea box. If the password matches, the script
> overwrites the webpage file with the new text you typed in the
> textarea.
>
> It works, but what security checks do I need to perform on it? I
> realize I need to sanitize the password field, how about the textarea
> field?
...
Well, I'm no security expert, but I would offer the following:
Depending upon your web server, you may be sufficiently restricted as to
where the user could read and place a file. You will want to either
validate the "file_path" to ensure a malicious user can't specify
something which might clobber part of your OS, or else verify for sure
that your web server itself restricts where the CGI script is permitted
to write sufficiently so you won't get hacked. Bear in mind that a user
could choose an executable just for grins if it happens to be available.
Even with typical restrictions on web browser CGI script access, the
user could still view your web site, as the stuff under the htdocs
directory is typically permitted. Hopefully cgi-bin is not accessible
-- otherwise the user could view your CGI scripts (and modify them once
he sees the password code).
Why don't you
use CGI;
when writing CGI scripts? It's easier, generates more correct and
portable HTML, etc.
Further comments below...
> Here's a snipet of the code below:
> ==================================
>
> $file_path=$incoming{'file_path'};
> $password=$incoming{'password'};
> @textarea=$incoming{'textarea'};
Not sure what you're trying to do here -- $incoming{'textarea'} returns
a scalar value, and you're assigning an array to that. The scalar value
is probably an array reference (since you don't show the rest of the
code, one must assume that, as an array can't be the value of a hash
element), so you probably want:
@textarea=@{$incoming{'textarea'}};
How far did you get with testing your code??
>
> # overwrite file if password matches with contents of @message
What's @message??
>
> if ($password eq "12345"){
> open(F, ">$file_path") ;
You will want to verify the success of your open -- *always* *always*
*always*!!!
> flock(F, 2); # lock file
By the time the open for write has occurred, your file is already
clobbered, so the flock will not be effective at preventing this process
from clobbering a write operation that might be going on in another
process. Read up on file locking. perldoc -q lock -- and all your
system's docs on flock, fcntl, lockf, etc etc.
> print F "@textarea";
----------^---------^
Useless use of quotes. See:
perldoc -q quoting
for why this is bad.
> close(F);
> }
>
The password check above does not apply to the code below. Are you sure
you want to read and print the contents of the file to a user without a
valid password? Depending on the restrictions of your server and the
permissions of the user under which your CGI script is running, the user
might have access to view more than you intend.
> # display web page to allow user to edit web page
>
> print "Content-type: text/html\n\n";
>
> print"<form action=/cgi-bin/edit_webpage.pl method=POST>
>
> <textarea name=message cols=75 rows=25>";
>
> open(F, "$file_path") ;
-------------^----------^
Useless use of quotes again. Also, no check on successful file opening
again.
> print <F>;
What happens if the file contains a bunch of HTML? Like maybe something
with the string </textarea> in it? You'll need to escape that stuff
from being interpreted as HTML by the brower. Also, you attempted to
flock() before; why do you think you don't have to worry about locks here?
>
> print"</textarea>";
>
> close(F);
>
> print' <input type="password" name="password"> ';
>
> print' <input type="submit" value="Edit"> ';
In short, without some fixes, what you have is probably "hacker heaven".
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: 19 Oct 2004 03:30:37 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Editable webpage code - security loopholes?
Message-Id: <Xns9586E4FC4FE60castleamber@130.133.1.4>
Bob Walton wrote:
> Duke of Hazard wrote:
[snip]
>> print F "@textarea";
> ----------^---------^
> Useless use of quotes. See:
>
> perldoc -q quoting
Not true, see:
perl -e "@a=qw(a b c d);print qq(@a)"
a b c d
perl -e "@a=qw(a b c d);print @a"
abcd
perl -e "@a=qw(a b c d);print \"@a\""
a b c d
In this case the quotes add some extra magic ;-)
[snip]
> In short, without some fixes, what you have is probably "hacker
> heaven".
No doubt about that.
--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Tue, 19 Oct 2004 00:06:08 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do I print http 404 not found header?
Message-Id: <2titb9F1vmn4eU1@uni-berlin.de>
nntp wrote:
>> Nothing is preventing you from doing both, is there?
>>
>> print "Status: 404 Not Found\n",
>> "Content-type: text/html; charset=ISO-8859-1\n",
>> "\n";
>
> Do you know where I can find the correct header for 404?
> You did
> print "Status: 404 Not Found\n",
> I did
> print "HTTP:/1.1 404 Not Found\n",
>
> I need to fake the header to make the brower think it is 404. The
> fact is I am using a script to give the error.
> So, when seombody visits at some.cgi?okok, I return a 404 header.
I thought that "Status: 404" was correct. Actually, I hope it is,
because I'm using it myself for similar purposes. ;-)
Your question is off topic here, and if you want to go into details with
respect to the HTTP specification, this is definitely the wrong place.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 18 Oct 2004 23:38:12 +0100
From: lesley_b_linux@yahoo.co.yuk
Subject: Re: How do I print http 404 not found header?
Message-Id: <m3mzyjtzxn.fsf@helmholtz.local>
Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
> nntp wrote:
> >> Nothing is preventing you from doing both, is there?
> >> print "Status: 404 Not Found\n",
> >> "Content-type: text/html; charset=ISO-8859-1\n",
> >> "\n";
> > Do you know where I can find the correct header for 404?
> > You did
> > print "Status: 404 Not Found\n",
> > I did
> > print "HTTP:/1.1 404 Not Found\n",
> > I need to fake the header to make the brower think it is 404. The
> > fact is I am using a script to give the error.
> > So, when seombody visits at some.cgi?okok, I return a 404 header.
>
> I thought that "Status: 404" was correct. Actually, I hope it is,
> because I'm using it myself for similar purposes. ;-)
>
> Your question is off topic here, and if you want to go into details with
> respect to the HTTP specification, this is definitely the wrong place.
>
> --
In a totally OT way :
From RFC2616
6.1 Status-Line
The first line of a Response message is the Status-Line, consisting
of the protocol version followed by a numeric status code and its
associated textual phrase, with each element separated by SP
characters. No CR or LF is allowed except in the final CRLF sequence.
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
Not sure about 'HTTP:/1.1' thought it might be 'HTTP/1.1'
Lesley
------------------------------
Date: Mon, 18 Oct 2004 23:46:37 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: How do I print http 404 not found header?
Message-Id: <Pine.LNX.4.61.0410182342470.32341@ppepc56.ph.gla.ac.uk>
On Tue, 19 Oct 2004, Gunnar Hjalmarsson wrote:
> I thought that "Status: 404" was correct.
Surely CGI.pm will take care of such details?
"Status: 404 reason text" is correct for a normal CGI response, but
not for an nph response.
> Actually, I hope it is,
> because I'm using it myself for similar purposes. ;-)
But if, for some reason, your script were to be repurposed as an
nph script, it would have to be re-cast.
> Your question is off topic here,
Right.
> and if you want to go into details with respect to the HTTP
> specification, this is definitely the wrong place.
But we have an FAQ which points to better places, and even to
some specifications. So we're not all bad :-}
------------------------------
Date: Mon, 18 Oct 2004 23:52:13 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: How do I print http 404 not found header?
Message-Id: <Pine.LNX.4.61.0410182351170.32341@ppepc56.ph.gla.ac.uk>
On Mon, 18 Oct 2004 lesley_b_linux@yahoo.co.yuk wrote:
> In a totally OT way :
>
> From RFC2616
RFC2616 is *not* a CGI specification.
------------------------------
Date: 19 Oct 2004 00:41:59 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How do I print http 404 not found header?
Message-Id: <slrncn8omn.img.abigail@alexandra.abigail.nl>
nntp (nntp@rogers.com) wrote on MMMMLXVI September MCMXCIII in
<URL:news:fsednb6186Krcu7cRVn-gA@rogers.com>:
{} print "HTTP/1.1 404 Not Found";
{} do I need
{} print "Content-type: text/html\n\n"; after it?
{}
{} Should I print
{} print "HTTP/1.1 404 Not Found";
{} print "locatioin: ...";
Do you actually have a Perl question?
Abigail
--
perl -wle 'eval {die [[qq [Just another Perl Hacker]]]};; print
${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]'
------------------------------
Date: Tue, 19 Oct 2004 01:26:36 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How do I print http 404 not found header?
Message-Id: <g3_cd.2616$EP4.1463@trnddc06>
nntp wrote:
> print "HTTP/1.1 404 Not Found";
> do I need
> print "Content-type: text/html\n\n"; after it?
No, print() statements in Perl are independent of each other. The first text
will print just fine without the second print() command.
Is there actually any programming language where you would have to issue a
second print command for the first one to print?
Now, if you _want_ to print the second line for your particular application
or not, that is a totally different questions and you should ask in a NG
that actually deals with your kind of application.
jue
------------------------------
Date: Tue, 19 Oct 2004 01:28:42 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: How do I print http 404 not found header?
Message-Id: <e5_cd.2620$EP4.1148@trnddc06>
Abigail wrote:
> nntp (nntp@rogers.com) wrote on MMMMLXVI September MCMXCIII in
> <URL:news:fsednb6186Krcu7cRVn-gA@rogers.com>:
> {} print "HTTP/1.1 404 Not Found";
> {} do I need
> {} print "Content-type: text/html\n\n"; after it?
[...]
> Do you actually have a Perl question?
He did. He wanted to know if in Perl you can use a single print() statement
or if they must come in pairs. Or something like that....
jue
------------------------------
Date: 19 Oct 2004 00:04:19 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments. No matches found.
Message-Id: <slrncn8mg3.img.abigail@alexandra.abigail.nl>
Vijai Kalyan (vijai.kalyan@gmail.com) wrote on MMMMLXVI September
MCMXCIII in <URL:news:18b36e50.0410181102.5c6afef0@posting.google.com>:
][ Hi,
][
][ I am learning perl so was working my way through the Camel book's
][ first chapter. I wrote the following program to match all C-style
][ multiline comments.
][
][ ---------------
][ $filename = "testinput.txt";
][
][ -e $filename or die "Invalid file. File does not exist. $!\n";
][ -r $filename or die "Invalid file. File is not readable. $!\n";
][ -f $filename or die "Invalid file. File is not a regular file. $!\n";
][ -T $filename or die "Invalid file. File is not a text file. $!\n";
][
][ open (FILE,"<$filename");
][
][ while ($line = <FILE>){
][ if($line =~ m/(\/\*[.|\n]*\*\/)/){
][ print "Found comment $1\n";
][ }
][ }
][ ---------------
][
][ For RE: m/(\/\*.*?\*\/)/
][
][ I got matches for multi-line comments containing no \n. For example:
][
][ /* ... */
][
][ but not
][
][ /*
][ */
Well, as long as you read in the file line-by-line and match each line
separately, you will *never* find a multiline match.
I'd write the program as follows (untested!):
use strict; # I'm still learning, I need strict.
use warnings; # Cause I ain't perfect.
my $filename = "testinput.txt";
my $content; # This will contain the content of the file.
# Always, always, always check the return value of your syscalls,
# and act accordingly on a failure.
# I also avoid globs as much as possible, and prefer to use
# autovivifying filehandles. Furthermore, while magical open
# can be quite useful, using three or more arg open will surprise
# you less.
open my $file => "<", $filename or die "Failed to open '$filename': $!";
# I want to do multiline matches, so I need to read in the entire
# file at once. Therefore, I undefine $/, but I do it in a small block.
{ local $/;
$content = <$file>; # Reads in the entire file.
}
if ($line =~ m!/( # Capture
[*] # A slash followed by a star (needs to be escaped)
[^*]* # Zero or more things that aren't stars.
(?: # Start a group
[*](?!/) # A star, not followed by a slash
[^*]* # Zero or more things that aren't stars.
) # End of group
* # Previous group can be repeated zero or more times.
[*]/ # Close the comment.
) # End capture.
!x) { # Extended comment.
print "Found comment: $1\n";
}
Or, with the use of modules (untested):
use strict;
use warnings;
use IO::All;
use Regexp::Common;
my $filename = "testinput.txt";
print "Found comment: $!\n" if
io ($filename) -> slurp =~ $RE {comment} {C} {-keep};
Abigail
--
perl -wle 'eval {die [[qq [Just another Perl Hacker]]]};; print
${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]'
------------------------------
Date: 18 Oct 2004 16:13:05 -0700
From: botfood@yahoo.com (dan baker)
Subject: Re: options to shrink-wrap a perl script
Message-Id: <13685ef8.0410181513.31da34fe@posting.google.com>
Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de> wrote in message news:<cl0p27$rq8$1@online.de>...
> dan baker wrote:
> > Abigail <abigail@abigail.nl> wrote in message news:<slrncn5l1v.img.abigail@alexandra.abigail.nl>...
> >
> >>You're looking for PAR.
> >>-------------------
> >
> >
> > ok, great, what and where is "PAR"?
> >
> > d
>
> Didn't it occur to you that it could be a module?
> -----------------
wow, what a passive-aggressive bunch! excuse me for not being a mind
reader. I suppose PAR could be a module, or some third party software,
or some acronym for something I hesitate to guess at.
doesn't occur to YOU that something like "check the PAR module
available on CPAN.org" would have demonstrated knowledge AND good
manners?
Now that I've poked around a little more.... any further comments on
the useability of pp versus perl2exe versus other choices?
d
------------------------------
Date: 19 Oct 2004 00:12:27 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: options to shrink-wrap a perl script
Message-Id: <slrncn8mvb.img.abigail@alexandra.abigail.nl>
dan baker (botfood@yahoo.com) wrote on MMMMLXVI September MCMXCIII in
<URL:news:13685ef8.0410181513.31da34fe@posting.google.com>:
`` Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de> wrote in message news:<cl0p27$rq8$1@online.de>...
`` > dan baker wrote:
`` > > Abigail <abigail@abigail.nl> wrote in message news:<slrncn5l1v.img.abigail@alexandra.abigail.nl>...
`` > >
`` > >>You're looking for PAR.
`` > >>-------------------
`` > >
`` > >
`` > > ok, great, what and where is "PAR"?
`` > >
`` > > d
`` >
`` > Didn't it occur to you that it could be a module?
`` > -----------------
``
`` wow, what a passive-aggressive bunch! excuse me for not being a mind
`` reader. I suppose PAR could be a module, or some third party software,
`` or some acronym for something I hesitate to guess at.
``
`` doesn't occur to YOU that something like "check the PAR module
`` available on CPAN.org" would have demonstrated knowledge AND good
`` manners?
`` Now that I've poked around a little more.... any further comments on
`` the useability of pp versus perl2exe versus other choices?
Let's see. You ask a question. You get an answer. You whine because the
answer doesn't spoonfeed you, and requires you to a little work yourself,
telling me how I should have answered the question. And then you ask
more question?
Do you really think you'd get an answer?
Muhahahahhahahhahaha.
*PLOINK*
Abigail
--
END {print "Hacker\n"}
CHECK {print "another "}
INIT {print "Perl " }
BEGIN {print "Just " }
------------------------------
Date: Tue, 19 Oct 2004 01:34:15 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: options to shrink-wrap a perl script
Message-Id: <ra_cd.4080$Ug4.2713@trndny01>
dan baker wrote:
> Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de> wrote in message news:<cl0p27$rq8$1@online.de>...
>
>>dan baker wrote:
>>
>>>Abigail <abigail@abigail.nl> wrote in message news:<slrncn5l1v.img.abigail@alexandra.abigail.nl>...
>>>
>>>
>>>>You're looking for PAR.
>>>>-------------------
>>>
>>>
>>>ok, great, what and where is "PAR"?
>>>
>>>d
>>
>>Didn't it occur to you that it could be a module?
>>-----------------
>
>
> wow, what a passive-aggressive bunch! excuse me for not being a mind
> reader. I suppose PAR could be a module, or some third party software,
> or some acronym for something I hesitate to guess at.
Googling "Perl PAR" got me the answer right away.
------------------------------
Date: Mon, 18 Oct 2004 18:49:57 -0400
From: Uri Guttman <uguttman@athenahealth.com>
Subject: Re: regex to clean path
Message-Id: <m3lle3myju.fsf@linux.local>
>>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:
TM> Yuri Shtil <yshtil@synopsys.com> wrote:
>> I need to clean the PATH variable from redundant entries:
>>
>> a:b:c:c:c:s:a
>> should become a:b:v:s.
>>
>> I have written a few line script that does it using hash.
>> Does anybody know an elegant oneliner using regex?
TM> A regex is not likely the best tool for this job.
while i agree a hash is the best way it does need some ordering control
as this is a path. so here is a regex solution.
echo 'a:b:b:c:d:c:s:a' | perl -lpe '1 while s/([^:]+)(.*):\1/$1$2/g'
a:b:c:d:s
and that isn't fully tested but it looks ok so far. it could use an
improvement to remove the 1 while part but i leave that as an exercise
uri
------------------------------
Date: 19 Oct 2004 00:20:52 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: regex to clean path
Message-Id: <slrncn8nf3.img.abigail@alexandra.abigail.nl>
Uri Guttman (uguttman@athenahealth.com) wrote on MMMMLXVI September
MCMXCIII in <URL:news:m3lle3myju.fsf@linux.local>:
`' >>>>> "TM" == Tad McClellan <tadmc@augustmail.com> writes:
`'
`' TM> Yuri Shtil <yshtil@synopsys.com> wrote:
`' >> I need to clean the PATH variable from redundant entries:
`' >>
`' >> a:b:c:c:c:s:a
`' >> should become a:b:v:s.
`' >>
`' >> I have written a few line script that does it using hash.
`'
`' >> Does anybody know an elegant oneliner using regex?
`'
`'
`' TM> A regex is not likely the best tool for this job.
`'
`' while i agree a hash is the best way it does need some ordering control
`' as this is a path. so here is a regex solution.
The solution Tad gave,
$_ = join ':', grep !$seen{$_}++, split /:/;
does preserve order.
`' echo 'a:b:b:c:d:c:s:a' | perl -lpe '1 while s/([^:]+)(.*):\1/$1$2/g'
`' a:b:c:d:s
`'
`' and that isn't fully tested but it looks ok so far. it could use an
`' improvement to remove the 1 while part but i leave that as an exercise
It ain't ok though:
echo "/bin:/usr/bin:/usr/local/bin" |\
perl -lpe '1 while s/([^:]+)(.*):\1/$1$2/g'
/binusr/binusr/local/bin
Abigail
--
perl -le 's[$,][join$,,(split$,,($!=85))[(q[0006143730380126152532042307].
q[41342211132019313505])=~m[..]g]]e and y[yIbp][HJkP] and print'
------------------------------
Date: Mon, 18 Oct 2004 18:37:02 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Top posting
Message-Id: <slrncn8ksu.2vf.tadmc@magna.augustmail.com>
bxb7668 <bxb7668@somewhere.nocom> wrote:
> "Jon Ericson" <Jon.Ericson@jpl.nasa.gov> wrote in message
> news:rcgvfd7rfea.fsf@Jon-Ericson.sdsio.prv...
>> merlyn@stonehenge.com (Randal L. Schwartz) writes:
>>
>> >>>>>> "Richard" == Richard S Beckett <spikeywan@bigfoot.com>
> writes:
>> >
>> > Richard> In other newsgroups they much prefer people to top post.
^^^^^^^^^^^ ^^^^^^^^
^^^^^^^^^^^ ^^^^^^^^
>> > Name one.
> Most of the Microsoft newsgroups that I trawl have about a 50-50 split
> between top and bottom posters.
Then it is not the type of example that was requested.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 18 Oct 2004 16:21:21 -0700
From: Jon Ericson <Jon.Ericson@jpl.nasa.gov>
Subject: Re: Top posting
Message-Id: <rcgekjvr4su.fsf@Jon-Ericson.sdsio.prv>
"bxb7668" <bxb7668@somewhere.nocom> writes:
> Most of the Microsoft newsgroups that I trawl have about a 50-50
> split between top and bottom posters. An example is
> microsoft.public.windowsxp.basics.
Somehow I don't think the Microsoft groups are the best examples. ;-)
> Personally I prefer top posting as long as the answer has enough
> context to understand the discussion.
Why? As a reader I much prefer inline posting (with irrelevant
material snipped). It's easier to follow a discussion in a logical
order when I land in the middle of a thread. As an author, it's a tad
more time-consuming, but worth the effort. A little discipline
improves the quality of writing in my experience.
Jon
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V10 Issue 7268
***************************************