[25017] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7267 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 18 18:07:10 2004

Date: Mon, 18 Oct 2004 15:05:09 -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: 7267

Today's topics:
    Re: Calling a perl script from an html doc <nospam@bigpond.com>
    Re: Calling a perl script from an html doc <flavell@ph.gla.ac.uk>
    Re: Calling a perl script from an html doc lesley_b_linux@yahoo.co.yuk
    Re: Calling a perl script from an html doc (Malcolm Dew-Jones)
    Re: Calling a perl script from an html doc <spamtrap@dot-app.org>
    Re: Calling a perl script from an html doc lesley_b_linux@yahoo.co.yuk
    Re: Calling a perl script from an html doc lesley_b_linux@yahoo.co.yuk
    Re: Calling a perl script from an html doc <eighner@io.com>
    Re: Calling a perl script from an html doc <tadmc@augustmail.com>
    Re: Calling a perl script from an html doc <spamtrap@dot-app.org>
    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? <noreply@gunnar.cc>
    Re: How do I print http 404 not found header? <nntp@rogers.com>
    Re: How to checking a file that writing is complete? <bik.mido@tiscalinet.it>
        m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline (Vijai Kalyan)
    Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multi <perl@my-header.org>
    Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multi <mritty@gmail.com>
    Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multi <mritty@gmail.com>
    Re: Recursive directory removing? (Gary E. Ansok)
        regex to clean path <yshtil@synopsys.com>
    Re: regex to clean path <Jon.Ericson@jpl.nasa.gov>
    Re: regex to clean path <bik.mido@tiscalinet.it>
    Re: regex to clean path <tadmc@augustmail.com>
    Re: regex to clean path <parv_@yahooWhereElse.com>
    Re: Top posting <Jon.Ericson@jpl.nasa.gov>
    Re: Top posting <bxb7668@somewhere.nocom>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 19 Oct 2004 04:50:13 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Calling a perl script from an html doc
Message-Id: <2tihj5F1t9hulU1@uni-berlin.de>

Spin wrote:

> Sorry, my fault for not being explicit enough.
> 
> There is no web server. There is a file server (samba) on which the html
> doc is located. The file server has perl installed, but no web server. And
> installing a web server is not an option.

Why not? You have Samba (ie you have linux/unix) but no Apache.
This is getting way off-topic for a Perl newsgroup.

gtoomey


------------------------------

Date: Mon, 18 Oct 2004 19:55:18 +0100
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: Re: Calling a perl script from an html doc
Message-Id: <Pine.LNX.4.61.0410181943010.31780@ppepc56.ph.gla.ac.uk>

On Mon, 18 Oct 2004, Spin sailed bravely forth atop a sea of TOFU:

> Sorry, my fault for not being explicit enough.
> 
> There is no web server. 

The CGI specification (which in general is off-topic for discussion on 
this group) specifies a software interface between a process ("CGI 
script") and a web server.

> installing a web server is not an option.

So if there is no web server, you have a "sound of one hand clapping" 
situation.  If there's no server, there can be no interface to it; 
ergo, no CGI.

If you have a closely-controlled situation in which there's some 
chance of the user consenting to their client software executing a 
retrieved script (in Perl or any other scripting language), then they 
*could*, thoretically, configure the browser to retrieve a Perl script 
from (say) a file://.... URL and execute it automatically.  But this 
would be rated as a very high-risk thing to do, and only applicable in 
very special circumstances. Of course, the script will then be 
executed on the client host, /not/ on your file server.

Maybe if you'd reveal what you're trying to achieve, instead of 
presenting a partially-formed solution to some unstated requirement, 
you might get some more-constructive answers.  But there'd better be 
some Perl significance in the question, or the locals around here are 
likely to get restive.  (A proper interleaved posting style would also 
stand you a better chance, as usual on big-8 usenet groups, by the 
way.)


------------------------------

Date: 18 Oct 2004 20:02:30 +0100
From: lesley_b_linux@yahoo.co.yuk
Subject: Re: Calling a perl script from an html doc
Message-Id: <m3fz4bvohk.fsf@helmholtz.local>

"Spin" <spam@spamhotmail.com> writes:

> How can I call a perl script from an html document when I have no web server
> (eg. Apache with its cgi-bin folder)? Our accounting system's reports are
> created as html files on a linux server, and they pop up in IE6 on our local
> Win2k workstations.

Maybe you need to re-think this and write a perl script that collects the data
and then write out to a text file the html you want to see (or should that be xhtml
these days?).  Look at CPAN modules and what's available to help you do that
on your local system.
> 
> What I want to do is call a perl script like this in my html: <img
> src="my_perl_script_gets_my_dynamic_image.pl">.
Perl doesn't work this way at all.  

You can use CGI with the Apache webserver and execute perl scripts either by
calling them directly or via form submission in a web page.  But you have to
have CGI installed with a web server (typically Apache) to do either of those things.
 
Note that PHP doesn't work the way you describe at all either.  PHP embedded in web
pages looks like this :-
<?
 ... php commands ...
?>

Nor does Javascript which looks like this 
<script language="Javascript>
<!--
 ... javascript commands ...
-->
</script>

An image is an image and the image tag is designed to display a png gif jpg or
other image source that can be displayed on a browser.  

As other posters have said this is not a perl question per se.
But if it gets you a pay rise or keeps you in your job then I claim my 10% ;)
And apologies to everyone else for even so much as mentioning those other
languages!!!

Regards

Lesley



------------------------------

Date: 18 Oct 2004 12:30:38 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: Calling a perl script from an html doc
Message-Id: <417419de@news.victoria.tc.ca>

Spin (spam@spamhotmail.com) wrote:
: How can I call a perl script from an html document when I have no web server
: (eg. Apache with its cgi-bin folder)? Our accounting system's reports are
: created as html files on a linux server, and they pop up in IE6 on our local
: Win2k workstations.

: What I want to do is call a perl script like this in my html: <img
: src="my_perl_script_gets_my_dynamic_image.pl">.

: Basically it's the equivalent right now of calling this page in my browser:
: file:///W:/reports/my_perl_script_etc.pl which only displays the contents of
: the script instead of processing it via perl. Can I use Javascript or
: something to make the server run the script through perl in order to display
: it as an image in my local html doc?


Without a server, there's basically one way to run your cgi script.

You must run it ahead of time via some kind of batch job, and save the
output in one of more files.  The html would then point to those files.

Whether this is practical depends especially on whether there only are a
small number of options that must be handled.



------------------------------

Date: Mon, 18 Oct 2004 16:00:42 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Calling a perl script from an html doc
Message-Id: <4YKdnZvGfqlpvencRVn-2g@adelphia.com>

lesley_b_linux@yahoo.co.yuk wrote:

> "Spin" <spam@spamhotmail.com> writes:
> 
>>What I want to do is call a perl script like this in my html: <img
>>src="my_perl_script_gets_my_dynamic_image.pl">.
> 
> Perl doesn't work this way at all.  

Nonsense.

> An image is an image and the image tag is designed to display a png gif jpg or
> other image source that can be displayed on a browser.  

A CGI, regardless of the language its written in, can return an 
image/whatever MIME type and image data, and a src attribute that points 
to such a CGI is perfectly valid and commonly used to generate "on the 
fly" images from map data and other dynamic applications.

As others have pointed out, the only thing preventing it from working 
here is the lack of a web server to execute the CGI.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: 18 Oct 2004 21:39:16 +0100
From: lesley_b_linux@yahoo.co.yuk
Subject: Re: Calling a perl script from an html doc
Message-Id: <m31xfvvk0b.fsf@helmholtz.local>

Sherm Pendley <spamtrap@dot-app.org> writes:

> lesley_b_linux@yahoo.co.yuk wrote:
> 
> > "Spin" <spam@spamhotmail.com> writes:
> >
> >>What I want to do is call a perl script like this in my html: <img
> >>src="my_perl_script_gets_my_dynamic_image.pl">.
> > Perl doesn't work this way at all.
> 
> Nonsense.
> 
> > An image is an image and the image tag is designed to display a png gif jpg or
> > other image source that can be displayed on a browser.
> 
> A CGI, regardless of the language its written in, can return an image/whatever
> MIME type and image data, and a src attribute that points to such a CGI is
> perfectly valid and commonly used to generate "on the fly" images from map
> data and other dynamic applications.
> 
Well, tyvm.  Had never considered using CGI and Perl in this way.  Very useful
piece of info.

> As others have pointed out, the only thing preventing it from working here is
> the lack of a web server to execute the CGI.
> 

Yes I read the previous posts and I pointed out in my previous post, that he
needs to think outside the box in order to solve his problem.  He doesn't need 
CGI and a webserver, he needs to write a perl script that collects the data
and then writes out the HTML page.  From the little the OP has stated I would
hazard a guess that the pages the Accounts Dept sees are built in this way
with the perl script creating them possibly triggered by some event somewhere
or done on request.

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.  Has there ever been any other language used with CGI or
was CGI specifically designed to work with Perl and nothing else?

Regards

Lesley



------------------------------

Date: 18 Oct 2004 21:46:08 +0100
From: lesley_b_linux@yahoo.co.yuk
Subject: Re: Calling a perl script from an html doc
Message-Id: <m3wtxnu54f.fsf@helmholtz.local>

lesley_b_linux@yahoo.co.yuk writes:
<snip>
> 
> 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.  Has there ever been any other language used with CGI or
> was CGI specifically designed to work with Perl and nothing else?
> 

Apologies, found my own answer at http://hoohoo.ncsa.uiuc.edu/cgi/intro.html

Regards

Lesley


------------------------------

Date: Mon, 18 Oct 2004 16:13:07 -0500
From: Lars Eighner <eighner@io.com>
Subject: Re: Calling a perl script from an html doc
Message-Id: <slrncn8caq.2qoa.eighner@goodwill.io.com>

In our last episode, 
<10n81aujp20bq23@corp.supernews.com>, 
the lovely and talented Spin 
broadcast on comp.lang.perl.misc:

> Sorry, my fault for not being explicit enough.

> There is no web server. There is a file server (samba) on which the html doc
> is located. The file server has perl installed, but no web server. And
> installing a web server is not an option.

If there is no server, then obviously this would have to be performed
by the client (i.e. browser).  There are not any other options.  HTML
is not a programming language, it cannot do anything itself.

Some browsers can do things like this.  For example, lynx has
lynxcgi to execute cgi without a server and lynxexec for other
executable links.  Consult a group particular to the browser you
want to use to determine if it is possible with that browser.  There
is no standard way of doing it, and it has nothing to do with HTML.

-- 
Lars Eighner -finger for geek code-  eighner@io.com http://www.io.com/~eighner/
      If it wasn't for muscle spasms, I wouldn't get any exercise at all.


------------------------------

Date: Mon, 18 Oct 2004 16:05:09 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Calling a perl script from an html doc
Message-Id: <slrncn8c05.1ug.tadmc@magna.augustmail.com>

lesley_b_linux@yahoo.co.yuk <lesley_b_linux@yahoo.co.yuk> wrote:


> Has there ever been any other language used with CGI or


Yes.


> was CGI specifically designed to work with Perl and nothing else?


No.


The CGI was not designed for any programming language.

It is an *interface*. Any language that can conform to the interface
that it specifies can be used as a CGI program.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Mon, 18 Oct 2004 17:29:49 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Calling a perl script from an html doc
Message-Id: <OZidnTwUfr1KqOncRVn-qg@adelphia.com>

lesley_b_linux@yahoo.co.yuk wrote:

> Sherm Pendley <spamtrap@dot-app.org> writes:
> 
>>A CGI, regardless of the language its written in, can return an image/whatever
>>MIME type and image data
> 
> Well, tyvm.  Had never considered using CGI and Perl in this way.  Very useful
> piece of info.

If you're interested in exploring the idea, have a look at the GD module 
and its relatives on CPAN. Very handy, especially for producing graphs.

Another interesting technique is a "two-stage" request. This is where a 
CGI produces HTML output with an <img ...> element in it. In the <img> 
element's src attribute, the CGI appends one or more of the parameters 
it received, thus passing them along to the CGI that will then use them 
to produce the image.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: 18 Oct 2004 20:29:48 +0100
From: lesley_b_linux@yahoo.co.yuk
Subject: Re: How do I print http 404 not found header?
Message-Id: <m37jpnvn83.fsf@helmholtz.local>

Robert Sedlacek <phaylon@dunkelheit.at> writes:

> nntp wrote:
> 
> > print "HTTP/1.1 404 Not Found";
> > do I need
> > [...]
> > Should I print
> > [...]
> 
> You could first say what you are trying to do. :) If you want to have a
> custom ErrorDocument, print out the Data you want to sent to the browser.
> And if this is html, a Content-Type set to text/html should be right.


I'd advise against a customised error document.  Nimda seized quite a few
servers up because they were sending customised 404 pages out.  A simple 404
not found http message left to the browser default display mechanism is
sufficient.

Regards

Lesley


------------------------------

Date: Mon, 18 Oct 2004 22:38:17 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do I print http 404 not found header?
Message-Id: <2tio5tF1vttfkU1@uni-berlin.de>

lesley_b_linux@yahoo.co.yuk wrote:
> Robert Sedlacek <phaylon@dunkelheit.at> writes:
>> nntp wrote:
>>> 
>>> print "HTTP/1.1 404 Not Found";
>>> do I need
>>> [...]
>>> Should I print
>>> [...]
>> 
>> You could first say what you are trying to do. :) If you want to
>> have a custom ErrorDocument, print out the Data you want to sent to
>> the browser. And if this is html, a Content-Type set to text/html
>> should be right.
> 
> I'd advise against a customised error document.  Nimda seized quite a
> few servers up because they were sending customised 404 pages out.  A
> simple 404 not found http message left to the browser default display
> mechanism is sufficient.

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";

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Mon, 18 Oct 2004 17:50:31 -0400
From: "nntp" <nntp@rogers.com>
Subject: Re: How do I print http 404 not found header?
Message-Id: <2NydndEDktZZp-ncRVn-jg@rogers.com>

>
> 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.




------------------------------

Date: Mon, 18 Oct 2004 23:03:18 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to checking a file that writing is complete?
Message-Id: <pv88n05jtrf7dqb5fjb6f3q3lse27bt6p9@4ax.com>

On Mon, 18 Oct 2004 00:44:23 +0100, Ben Morrow <usenet@morrow.me.uk>
wrote:

>fs iff stat returns the same first member for both. Be aware that 'fake'
>filesystems (such as Linux' usbfs (/proc/bus/usb) and tmpfs (often used
>for /dev and /dev/shm)) may return 'undef' for this member, so be

<OT>
I think that nowadays it is common for /dev to be on devfs, which btw
doesn't contradict the key point you're addressing, since it is just
as 'fake'.
</OT>


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: 18 Oct 2004 12:02:20 -0700
From: vijai.kalyan@gmail.com (Vijai Kalyan)
Subject: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments. No matches found.
Message-Id: <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 

/*
 */

I looked up "." and found that . won't match a \n. So, I modified the
RE to

m/(\/\*[.|\n]*\*\/)/

but this didn't help either. If I got it right, the above RE says"

"Match a /*. After that match as many characters as possible but match
any character or newlines followed by a */"

So, this should match 

/* ... */

as well as

/*
 */

but, I find that now, nothing is matched. 

Any suggestions?

thanx,

-vijai.


------------------------------

Date: Mon, 18 Oct 2004 21:26:03 +0200
From: Matija Papec <perl@my-header.org>
Subject: Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments. No matches found.
Message-Id: <mu58n0h8p1bk1t17jdnk68pvscq19im4as@4ax.com>

X-Ftn-To: Vijai Kalyan 

vijai.kalyan@gmail.com (Vijai Kalyan) wrote:
>/* ... */
>
>as well as
>
>/*
> */
>
>but, I find that now, nothing is matched. 
>
>Any suggestions?

CPAN has nice module for matching common regexps, that is if you don't
insist on bondage/your own solution. :)

http://search.cpan.org/~abigail/Regexp-Common-2.117/lib/Regexp/Common.pm
Regexp::Common::comment



-- 
Matija


------------------------------

Date: Mon, 18 Oct 2004 15:22:17 -0400
From: Paul Lalli <mritty@gmail.com>
To: Vijai Kalyan <vijai.kalyan@gmail.com>
Subject: Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments. No matches found.
Message-Id: <417417E9.8090005@gmail.com>

Vijai Kalyan wrote:
> 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.

<snip>

> ---------------
> 
> For RE: m/(\/\*.*?\*\/)/
> 
> I got matches for multi-line comments containing no \n. For example:
> 
> /* ... */
> 
> but not 
> 
> /*
>  */
> 
> I looked up "." and found that . won't match a \n. So, I modified the
> RE to
> 
> m/(\/\*[.|\n]*\*\/)/
> 
> but this didn't help either. If I got it right

Obviously, you didn't get it right.   You're using a character class 
which matches an actual period, vertical-bar, or a newline.  Keep 
reading the documentation in your Camel or in
perldoc perlretut
perldoc perlre

The correct way to get the period to match a newline is to add the /s 
modifier onto your regexp.

> Any suggestions?

1) Keep reading the documentation.  (That's not a criticism, it's a 
compliment - too many questioners in this group don't read and just 
expect everyone to read the docs to them).  Specifically, you need to 
read up on character classes and regexp modifiers.
2) if you're doing this as a learning exercise, make . match the newline 
the correct way.
3) If you're doing this for production code, don't bother rolling your 
own solution - check the Perl FAQ before doing something like this:
perldoc -q comment

Paul Lalli


------------------------------

Date: Mon, 18 Oct 2004 15:23:20 -0400
From: Paul Lalli <mritty@gmail.com>
Subject: Re: m/(\/\*[.|\n]*\*\/)/ to try and match C-Style multiline comments. No matches found.
Message-Id: <cl157a$5mg$2@misc-cct.server.rpi.edu>

Vijai Kalyan wrote:

> 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.
> 
<snip>
> ---------------
> 
> For RE: m/(\/\*.*?\*\/)/
> 
> I got matches for multi-line comments containing no \n. For example:
> 
> /* ... */
> 
> but not 
> 
> /*
>  */
> 
> I looked up "." and found that . won't match a \n. So, I modified the
> RE to
> 
> m/(\/\*[.|\n]*\*\/)/
> 
> but this didn't help either. If I got it right


Obviously, you didn't get it right.   You're using a character class 
which matches an actual period, vertical-bar, or a newline.  Keep 
reading the documentation in your Camel or in
perldoc perlretut
perldoc perlre

The correct way to get the period to match a newline is to add the /s 
modifier onto your regexp.

 > Any suggestions?

1) Keep reading the documentation.  (That's not a criticism, it's a 
compliment - too many questioners in this group don't read and just 
expect everyone to read the docs to them).  Specifically, you need to 
read up on character classes and regexp modifiers.
2) if you're doing this as a learning exercise, make . match the newline 
the correct way.
3) If you're doing this for production code, don't bother rolling your 
own solution - check the Perl FAQ before doing something like this:
perldoc -q comment

Paul Lalli


------------------------------

Date: Mon, 18 Oct 2004 21:37:59 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: Recursive directory removing?
Message-Id: <cl1d3n$il$1@naig.caltech.edu>

In article <n1bcd.6376$1p.5442@nntpserver.swip.net>,
MM <dobedidoo@yahoo.se> wrote:
>Can anyone give me some hint on how I in a perl script can remove all
>directories, subdirectories, and files (recursively) in a specific directory
>that I state? Pretty much like the 'rm -fR' system command, except that I do
>not want to use the 'system' command. I looked at the 'unlink' function, but
>I could not see how this can be used to remove subdirectories as well.

Check out the rmtree() function in the File::Path module (which you
should already have as part of the Perl installation).

perldoc File::Path

-- Gary Ansok


------------------------------

Date: Mon, 18 Oct 2004 12:34:40 -0700
From: "Yuri Shtil" <yshtil@synopsys.com>
Subject: regex to clean path
Message-Id: <10n86m9jvqce312@corp.supernews.com>

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?




------------------------------

Date: Mon, 18 Oct 2004 13:39:53 -0700
From: Jon Ericson <Jon.Ericson@jpl.nasa.gov>
Subject: Re: regex to clean path
Message-Id: <rcgr7nvrc9y.fsf@Jon-Ericson.sdsio.prv>

"Yuri Shtil" <yshtil@synopsys.com> writes:

> I need to clean the PATH variable from redundant entries:
>
> a:b:c:c:c:s:a
> should become a:b:v:s.

I think you meant a:b:c:s.

> I have written a few line script that does it using hash.
>
> Does anybody know an elegant oneliner using regex?

$ perl -e '%h = map {$_ => 1} split(/:/, "a:b:c:c:c:s:a");\
  print join(":", sort keys %h), "\n"'

Finding the regex is left as an exercise for the reader. ;-)  Sorting
alphabeticaly might not be what you want.

Seriously, the regex language probably is *not* the best choice for a
problem like this.  What's wrong with using a hash?

Jon


------------------------------

Date: Mon, 18 Oct 2004 23:03:27 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: regex to clean path
Message-Id: <cgb8n0d20l880tbnr1p50jm3nn8emg19f2@4ax.com>

On Mon, 18 Oct 2004 12:34:40 -0700, "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.

It should/could be a one-line script except possibly for the hash
declaration. Anyway, what's wrong with it?

>Does anybody know an elegant oneliner using regex?

It's not clear what you mean by "using regex": assuming you refer to a
substitution operator... I just wouldn't do it, but if i really have
to, then

  s/(^|:)([^:]*)/$seen{$2}++?'':$1.$2/ge;

Of course if you use this in anything that is not a simple one liner
and C<use strict> as one always should, then you must also have
C<my %seen>.


HTH,
Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Mon, 18 Oct 2004 16:11:36 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: regex to clean path
Message-Id: <slrncn8cc8.1ug.tadmc@magna.augustmail.com>

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.


If you had shown it to use, we could have suggested improvements.

But you didn't, so we can't.


> Does anybody know an elegant oneliner using regex?


A regex is not likely the best tool for this job.

I'd just to it using a hash:

   $_ = join ':', grep !$seen{$_}++, split /:/;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Mon, 18 Oct 2004 16:45:22 -0500
From: parv <parv_@yahooWhereElse.com>
Subject: Re: regex to clean path
Message-Id: <slrncn8eg8.g06.parv_@localhost.holy.cow>

in message <10n86m9jvqce312@corp.supernews.com>,
wrote Yuri Shtil ...

> 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?

No regex, just split(); on top of that, in more than one line ...

  #!/usr/local/bin/perl

  use warnings;
  use strict;

  my @paths = @ARGV;
  printf "Unordered: %s\n\nOrdered: %s\n"
  , ${ make_path( @paths ) }
  , ${ make_path_ordered( @paths ) }
  ;

  #  Make path string from given list of array references or strings
  sub make_path
  { my @paths = @_;

    my %uniq;
    map $uniq{$_} = undef , @{ split_path( [@paths] ) };
    keep_usable(\%uniq);

    return delimit_path( [ keys %uniq ] ) ;
  }

  #  Make ordered path -- as indicated by the first position of each unique
  #  path -- string from given list of array references or just plain
  #  strings
  sub make_path_ordered
  { my @paths = @_;

    my %uniq;
    { my $i = 0;
      map $uniq{$_} = !exists $uniq{$_} ? $i++ : $uniq{$_}
        , @{ split_path( [ @paths ]) };
    }
    keep_usable(\%uniq);

    return
      delimit_path
      ( [ map $_->[0]
          , sort { $a->[1] <=> $b->[1] }
              map [$_ , $uniq{$_} ] , keys %uniq
        ]
      ) ;
  }

  #  Make ':' delimited string from given path array reference
  sub delimit_path
  { my ($paths) = @_;
    return \join ':' , @{$paths};
  }

  #  Input is a list of paths, each element being an array reference or a
  #  string
  sub split_path
  { my ($paths) = @_;
    return
      [ map split( /[:\s]/ , ref $_ ? @$_ : $_ ) , @{$paths} ];
  }

  #  Keep only read/execute-able directories from the keys of given hash
  #  reference
  sub keep_usable
  { my ($hash) = @_;
    foreach ( keys %{$hash} )
    { delete $hash->{$_} unless -d $_ || -x $_;
    }
  }

  __END__


  - parv

-- 
As nice it is to receive personal mail, too much sweetness causes
tooth decay.  Unless you have burning desire to contact me, do not do
away w/ WhereElse in the address for private communication.



------------------------------

Date: Mon, 18 Oct 2004 12:32:29 -0700
From: Jon Ericson <Jon.Ericson@jpl.nasa.gov>
Subject: Re: Top posting
Message-Id: <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.

rec.crafts.scrapbooks

But I can't think of any technical groups.  My guess is that groups
eventually migrate to inline posting as the value of precision becomes
apparent.

Jon


------------------------------

Date: Mon, 18 Oct 2004 20:57:40 GMT
From: "bxb7668" <bxb7668@somewhere.nocom>
Subject: Re: Top posting
Message-Id: <I5ssw3.J2w@news.boeing.com>

"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.
>
> rec.crafts.scrapbooks
>
> But I can't think of any technical groups.  My guess is that groups
> eventually migrate to inline posting as the value of precision
becomes
> apparent.
>
> Jon

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.

Personally I prefer top posting as long as the answer has enough
context to understand the discussion.




------------------------------

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 7267
***************************************


home help back first fref pref prev next nref lref last post