develooper Front page | perl.beginners | Postings from April 2002

RE: 2 questions

Thread Previous | Thread Next
From:
Timothy Johnson
Date:
April 2, 2002 16:09
Subject:
RE: 2 questions
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D5002581233@SMILEY

To check if a file exists, you might be better off using the built-in file
tests.

if(-e $file){
   Do something...
}else{
   Do something else...
}


-----Original Message-----
From: Eric Plowe [mailto:eric@hypoflux.net]
Sent: Tuesday, April 02, 2002 4:03 PM
To: Glenn Cannon
Cc: beginners@perl.org
Subject: Re: 2 questions


Glenn..

to print the current directory name...try

---
#!/usr/bin/perl -w
use strict;
use Cwd;

$curr = cwd();
print "$curr\n";

easy enough
----

this is bit longer example.. you could prolly do it another way..but 
this is how *I* would do it
---
#!/usr/bin/perl -w
use strict;
use Cwd;

my $curr = cwd(); (sets $curr to the current directory the script is 
being ran from)
my @files = &getdirfiles();
my $nof = "the name of the file you're looking to see exists";

if (grep /^$nof$/, @files) {
    print "File $nof exists\n";
    $nof = '';
} elsif (!grep /^$nof$/, @files) {
    print "File $nof does not exsist";
    $nof = '';
}

sub getdirfiles {
opendir(CURRDIR, $curr) or die "Can't open directory\n";
@files = readdir(CURRDIR);
closedir(CURRDIR);
return @files;
}
----

hope those examples help...

~Eric
On Tuesday, April 2, 2002, at 06:20 PM, Glenn Cannon wrote:

> Hi all,
>
> Couple of questions from a newbie...
>
> 1)  How can I print the current directory name?
>
> 2)  How can I check to see if a file I know the name of exists?
>
> Thx,
>
> Glenn
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About