Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > using wildcards with -e

Reply
Thread Tools

using wildcards with -e

 
 
DMB
Guest
Posts: n/a
 
      06-27-2007
I have the following code:

if (-e $done_dir."BOB.DOC") {
print "found it\n";
} else {
print "nope\n";
}

where $done_dir contains the directory path of the file I'm looking
for.

I'd like to make it so that it doesn't matter what the filename is.
I'm only interested in the suffix. I was thinking along the lines
of...

if (-e $done_dir."*.DOC") {
print "found it\n";
} else {
print "nope\n";
}

but of course this does not work in perl. I'm sure there is an easy
solution to this, but I can't seem to find a example online or in any
of my books of using -e and wildcards.

Thank you for your assistance.

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      06-27-2007
DMB <(E-Mail Removed)> writes:

> I'd like to make it so that it doesn't matter what the filename is.
> I'm only interested in the suffix. I was thinking along the lines
> of...
>
> if (-e $done_dir."*.DOC") {
> print "found it\n";
> } else {
> print "nope\n";
> }


You might want to look at the glob function which expands these kinds
of wildcards into a list of files. So something like

if (grep { -e $_ } glob("$donedir/*.DOC") ) {
print "Found it";
}

//Makholm


 
Reply With Quote
 
 
 
 
DMB
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 4:13 pm, Peter Makholm <(E-Mail Removed)> wrote:
> DMB <(E-Mail Removed)> writes:
> > I'd like to make it so that it doesn't matter what the filename is.
> > I'm only interested in the suffix. I was thinking along the lines
> > of...

>
> > if (-e $done_dir."*.DOC") {
> > print "found it\n";
> > } else {
> > print "nope\n";
> > }

>
> You might want to look at the glob function which expands these kinds
> of wildcards into a list of files. So something like
>
> if (grep { -e $_ } glob("$donedir/*.DOC") ) {
> print "Found it";
>
> }
>
> //Makholm


Great! I'm reading on glob now. I'm not sure I fully understand it
yet, but your example worked perfectly when I tested it.

Thank you.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-27-2007
Peter Makholm wrote:
> DMB <(E-Mail Removed)> writes:
>
>> I'd like to make it so that it doesn't matter what the filename is.
>> I'm only interested in the suffix. I was thinking along the lines
>> of...
>>
>> if (-e $done_dir."*.DOC") {
>> print "found it\n";
>> } else {
>> print "nope\n";
>> }

>
> You might want to look at the glob function which expands these kinds
> of wildcards into a list of files. So something like
>
> if (grep { -e $_ } glob("$donedir/*.DOC") ) {
> print "Found it";
> }


You shouldn't need the grep, this should work:

if ( glob "$donedir/*.DOC" ) {
print "Found it";
}


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      06-27-2007
On Wed, 27 Jun 2007 21:07:40 -0000, DMB <(E-Mail Removed)> wrote:

>if (-e $done_dir."*.DOC") {
> print "found it\n";
>} else {
> print "nope\n";
>}
>
>but of course this does not work in perl. I'm sure there is an easy
>solution to this, but I can't seem to find a example online or in any
>of my books of using -e and wildcards.


if (grep -e, glob "$done_dir*.DOC") {


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,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      06-27-2007
On Wed, 27 Jun 2007 22:18:02 GMT, "John W. Krahn" <(E-Mail Removed)>
wrote:

>You shouldn't need the grep, this should work:
>
>if ( glob "$donedir/*.DOC" ) {
> print "Found it";
>}


D'Oh! Just as obvious as I also missed that!


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,
 
Reply With Quote
 
Peter Makholm
Guest
Posts: n/a
 
      06-28-2007
"John W. Krahn" <(E-Mail Removed)> writes:

>> if (grep { -e $_ } glob("$donedir/*.DOC") ) {
>> print "Found it";
>> }

>
> You shouldn't need the grep, this should work:
>
> if ( glob "$donedir/*.DOC" ) {
> print "Found it";
> }


How could I miss that. I didn't even think about what kind of file
test was being used.

//Makholm
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      06-28-2007
On Jun 27, 6:18 pm, "John W. Krahn" <(E-Mail Removed)> wrote:
> Peter Makholm wrote:
> > DMB <(E-Mail Removed)> writes:

>


> >> if (-e $done_dir."*.DOC") {
> >> print "found it\n";
> >> } else {
> >> print "nope\n";
> >> }

>
> > You might want to look at the glob function which expands these
> > kinds of wildcards into a list of files. So something like

>
> > if (grep { -e $_ } glob("$donedir/*.DOC") ) {
> > print "Found it";
> > }

>
> You shouldn't need the grep, this should work:
>
> if ( glob "$donedir/*.DOC" ) {
> print "Found it";
> }


Danger, danger Will Robinson!! That bit of code will work only if
both of the following are true:
1) The string actually does contain shell meta characters.
2) The block is not being used in a loop.

If you later change your "pattern" to actualy be a single file, your
code will break. This is especially a problem if you're reading the
pattern from the user. Observe:

~/temp $ ls
~/temp $ perl -le'print for glob("foo.*")'
~/temp $ perl -le'print for glob("foo.bar")'
foo.bar
~/temp $

If the code is being called in a loop of some kind, and there are, say
N files that match the pattern, your if statement will fail every (N
+1)th time. Observe:

~/temp $ touch foo.1 foo.2 foo.3
~/temp $ perl -le'
for (1..10) {
if (glob("foo.*")) {
print "yes"
} else {
print "no"
}
}
'
yes
yes
yes
no
yes
yes
yes
no
yes
yes


All things considered, it's better to stick with the grep, and thereby
call glob() in a scalar context, explicitly testing the existance of
each return value.

Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      06-28-2007
On Jun 28, 1:12 pm, Paul Lalli <(E-Mail Removed)> wrote:

> All things considered, it's better to stick with the grep, and
> thereby call glob() in a scalar context, explicitly testing the
> existance of each return value.


s/scalar/list/;

Paul Lalli

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      06-28-2007
On Thu, 28 Jun 2007 10:12:29 -0700, Paul Lalli <(E-Mail Removed)>
wrote:

>If you later change your "pattern" to actualy be a single file, your
>code will break. This is especially a problem if you're reading the
>pattern from the user. Observe:


Oh, yeah, and there' brace expansion too. Perhaps it was not that bad
to leave the -e in after all...


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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disadvantage of using wildcards in import statement. Dipankar Java 17 08-01-2009 02:36 PM
Using wildcards with Popen in the Subprocess module William Hudspeth Python 2 03-15-2007 07:47 PM
download multiple files using wildcards with WebClient.DownloadFile() Web learner ASP .Net 0 04-18-2006 10:08 PM
Using wildcards... Harlin Seritt Python 4 05-02-2005 10:00 PM
Struts: Using Wildcards in ActionMapping Josh Martin Java 6 11-23-2003 06:06 PM



Advertisments