Newbie Perl question

Newbie Perl question

am 16.08.2007 18:57:37 von christopher.prowse

Hi experts,

A quick (I hope) question...

I want to be able to verify if any directory that matches a simple
regexp exists, but using the -d flag. So for example, I would want:

if (-d "/opt/*abc") {
print "Directory exists\n";
};

to print "Directory exists" if there are any directories such as "/opt/
abc", "/opt/123abc", "/opt/asdfghabc", and only fail the condition if
nothing meets it. More than one directory conforming to the regexp
should also successfully print "Directory exists".

Thanks in advance,
-Chris

Re: Newbie Perl question

am 16.08.2007 19:14:37 von xhoster

christopher.prowse@gmail.com wrote:
> Hi experts,
>
> A quick (I hope) question...
>
> I want to be able to verify if any directory that matches a simple
> regexp exists, but using the -d flag. So for example, I would want:
>
> if (-d "/opt/*abc") {

That isn't a regexp (or at least not a Perl regexp), it is shell expansion
or a glob.

> print "Directory exists\n";
> };

if (grep -d, glob "/opt/*abc") {


That doesn't stop upon finding the first directory. If that bothers you,
you can use List::Util::first.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: Newbie Perl question

am 16.08.2007 19:15:04 von nobull67

On Aug 16, 5:57 pm, christopher.pro...@gmail.com wrote:

> Subject: Newbie Perl question

Please put the subject of your post in the Subject of your post.

To do otherwise is very rude.

Re: Newbie Perl question

am 16.08.2007 19:21:55 von cprowse

On 16 Aug, 18:15, Brian McCauley wrote:
> On Aug 16, 5:57 pm, christopher.pro...@gmail.com wrote:
>
> > Subject: Newbie Perl question
>
> Please put the subject of your post in the Subject of your post.
>
> To do otherwise is very rude.

Apologies - clearly "newbie" applies not just to Perl. Suspect I'm
posting incorrectly to the top or bottom as well.

Xho >> thanks for the clarification and answer, should do exactly what
I want.

Kind Regards,
-Chris

Re: Newbie Perl question

am 16.08.2007 19:28:56 von merlyn

>>>>> "christopher" == christopher prowse writes:

christopher> I want to be able to verify if any directory that matches a simple
christopher> regexp exists, but using the -d flag. So for example, I would want:

christopher> if (-d "/opt/*abc") {
christopher> print "Directory exists\n";
christopher> };

The -d doesn't glob. You need to glob, to glob. :)

my @interesting_dirs = glob "/opt/*abc";
if (@interesting_dirs) {
print "they exist!\n";
}

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

Re: Newbie Perl question

am 16.08.2007 19:44:50 von jurgenex

christopher.prowse@gmail.com wrote:
> I want to be able to verify if any directory that matches a simple
> regexp exists, but using the -d flag. So for example, I would want:

Do you mean "but using" or "by using"?
"By using" doesn't work because -d (like all of the file test operators)
takes a file name as argument, not a RE.

> if (-d "/opt/*abc") {
> print "Directory exists\n";
> };
>
> to print "Directory exists" if there are any directories such as
> "/opt/ abc", "/opt/123abc", "/opt/asdfghabc", and only fail the
> condition if nothing meets it. More than one directory conforming to
> the regexp should also successfully print "Directory exists".

Actually, I think this is a non-trivial task.

If you were asking for shell expansion, then the issue would be simple. Just
do a glob() to get all file names and then a grep() with -d to filter for
the directories.

However, glob() doesn't do REs and you specifically asked for REs. I have no
idea how to list any file names based on REs except by walking the directory
tree using e.g. File::Find which of course is slow and cumbersome.

jue

Re: Newbie Perl question

am 16.08.2007 19:49:07 von jurgenex

Randal L. Schwartz wrote:
>>>>>> "christopher" == christopher prowse
>>>>>> writes:
>
> christopher> I want to be able to verify if any directory that
> matches a simple christopher> regexp exists, but using the -d flag.
> So for example, I would want:
>
> christopher> if (-d "/opt/*abc") {
> christopher> print "Directory exists\n";
> christopher> };
>
> The -d doesn't glob. You need to glob, to glob. :)

Well, glob does shell filename globbing only, it doesn't do RE matching.
And the OP was explicitely asking for RE matching which makes the task a lot
more complicated.

jue

Re: Newbie Perl question

am 16.08.2007 19:57:56 von cprowse

On 16 Aug, 18:49, "Jürgen Exner" wrote:
> Randal L. Schwartz wrote:
> >>>>>> "christopher" == christopher prowse
> >>>>>> writes:
>
> > christopher> I want to be able to verify if any directory that
> > matches a simple christopher> regexp exists, but using the -d flag.
> > So for example, I would want:
>
> > christopher> if (-d "/opt/*abc") {
> > christopher> print "Directory exists\n";
> > christopher> };
>
> > The -d doesn't glob. You need to glob, to glob. :)
>
> Well, glob does shell filename globbing only, it doesn't do RE matching.
> And the OP was explicitely asking for RE matching which makes the task a =
lot
> more complicated.
>
> jue

Unfortunately, OP clearly didn't understand his own question.
Apologies, I clearly worded something more confusing than I meant.
Xho's solution seems to do the job, I just want to know if any
directory exists that meets my condition (which I unfortunately called
a regexp, mostly because it has an * in it :S ). Thanks all for your
quick responses.

Re: Newbie Perl question

am 16.08.2007 20:52:44 von merlyn

>>>>> "Jürgen" == Jürgen Exner writes:

Jürgen> However, glob() doesn't do REs and you specifically asked for REs. I
Jürgen> have no idea how to list any file names based on REs except by walking
Jürgen> the directory tree using e.g. File::Find which of course is slow and
Jürgen> cumbersome.

There are some evil Unix Intro texts (usually random websites) that call
globbing "regular expressions". Boo. Hiss. But it means that some newbies
that are getting the wrong idea, and then spreading the bad meme.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

Re: Newbie Perl question

am 16.08.2007 21:55:23 von Michele Dondi

On 16 Aug 2007 17:14:37 GMT, xhoster@gmail.com wrote:

>> if (-d "/opt/*abc") {
>
>That isn't a regexp (or at least not a Perl regexp), it is shell expansion
>or a glob.

Well, strictly speaking it's a string, period. More loosely, it *can*
be a regex, just not meaning what the OP supposes.


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

Re: Newbie Perl question

am 16.08.2007 21:57:18 von Michele Dondi

On Thu, 16 Aug 2007 17:49:07 GMT, "Jürgen Exner"
wrote:

>Well, glob does shell filename globbing only, it doesn't do RE matching.
>And the OP was explicitely asking for RE matching which makes the task a lot
>more complicated.

A lot? Well nothing that a simple grep() can't handle. Anyway the OP
explicitly asked about RE matching, but *probably* meant globbing.


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

Re: Newbie Perl question

am 16.08.2007 21:59:11 von Michele Dondi

On 16 Aug 2007 17:14:37 GMT, xhoster@gmail.com wrote:

>if (grep -d, glob "/opt/*abc") {
>
>
>That doesn't stop upon finding the first directory. If that bothers you,
>you can use List::Util::first.

A common error in which I recently got caught too: glob() does -d (in
this case) anyway. So grep -d is redundant.


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

Re: Newbie Perl question

am 16.08.2007 22:05:29 von Michele Dondi

On Thu, 16 Aug 2007 21:59:11 +0200, Michele Dondi
wrote:

>>if (grep -d, glob "/opt/*abc") {
>>
>>
>>That doesn't stop upon finding the first directory. If that bothers you,
>>you can use List::Util::first.
>
>A common error in which I recently got caught too: glob() does -d (in
>this case) anyway. So grep -d is redundant.

D'Oh! I'm a moron. Ignore this...


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

Re: Newbie Perl question

am 16.08.2007 23:44:38 von jurgenex

Michele Dondi wrote:
> On Thu, 16 Aug 2007 17:49:07 GMT, "Jürgen Exner"
> wrote:
>
>> Well, glob does shell filename globbing only, it doesn't do RE
>> matching. And the OP was explicitely asking for RE matching which
>> makes the task a lot more complicated.
>
> A lot? Well nothing that a simple grep() can't handle.

True. But how do you get that initial list of file/directory names that you
feed into grep() without generating the full list of all files in all
directories recursively?
I couldn't come up with anything better than the either that or to use
something like File::Find to filter while traversing the tree.

jue

Re: Newbie Perl question

am 17.08.2007 11:19:32 von Michele Dondi

On Thu, 16 Aug 2007 21:44:38 GMT, "Jürgen Exner"
wrote:

>>> Well, glob does shell filename globbing only, it doesn't do RE
>>> matching. And the OP was explicitely asking for RE matching which
>>> makes the task a lot more complicated.
>>
>> A lot? Well nothing that a simple grep() can't handle.
>
>True. But how do you get that initial list of file/directory names that you
>feed into grep() without generating the full list of all files in all
>directories recursively?

Well, I can't. Of course I can't: grep() takes a list: since we do not
have lazy list evaluation (yet) the list must be generated in advance,
in some way or another. In that case I wouldn't do the check in a grep
but directly in the finding code. OTOH as most of use understood it,
the OP *didn't* want to match *recursively*.

>I couldn't come up with anything better than the either that or to use
>something like File::Find to filter while traversing the tree.

Indeed.


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