Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl file exists check

111 views
Skip to first unread message

jaceke

unread,
Nov 10, 2016, 5:45:03 AM11/10/16
to begi...@perl.org
Hi,
how can I check if file exist or not ?
 
Here's a short test/example:
 
if (-e '/etc/passwd')
{
printf "File exist !\n";
} else {
printf "File not exist !\n";
}
 
That works great !
 
but next a short test/example:
 
my $f1 = '/etc/passwd';
 
if (-e $f1)
{
printf "File exist !\n";
} else {
printf "File not exist !\n";
}
 
and that NOT working ! Why ?
 
I use quotes because my path contain special characters.
 
Ragards,
MattX

jaceke

unread,
Nov 10, 2016, 6:30:03 AM11/10/16
to begi...@perl.org, Jim Gibson
This example will be better :

#!/usr/bin/perl
use strict;
use File::Basename;
use utf8;

sub escMe($) {
my $f1 = shift;
my $f2 = '/etc/passwd';
my $f3 = "'/etc/passwd'";
my $f4 = '/etc/passwd';

$f1 = "'" . $f1 . "'";

if($f1 eq $f2) {
print("Files are same $f1$f2\n");
} else {
print("Files are NOT same $f1$f2\n");
}

if (-e $f1)
{
printf "File exist $f1 !\n";
} else {
printf "File not exist $f1 !\n";
}

if (-e '$f2')
{
printf "File exist $f2 !\n";
} else {
printf "File not exist $f2 !\n";
}

if (-e '$f3')
{
printf "File exist $f3 !\n";
} else {
printf "File not exist $f3 !\n";
}

if (-e '$f4')
{
printf "File exist $f4 !\n";
} else {
printf "File not exist $f4 !\n";
}

if (-e '/etc/passwd')
{
printf "File exist !\n";
} else {
printf "File not exist !\n";
}

return $f1;
}

escMe("/etc/passwd");



Output:
Files are NOT same '/etc/passwd'/etc/passwd
File not exist '/etc/passwd' !
File not exist /etc/passwd !
File not exist '/etc/passwd' !
File not exist /etc/passwd !
File exist !


W dniu 2016-11-10 11:52:46 użytkownik Jim Gibson <jimsg...@gmail.com> napisał:
> Both of those versions work on my system. There is no reason for the version using a scalar variable holding the file path not to work.
>
> Can you post a complete, short, working program that you think does not work?
>
>
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>



jaceke

unread,
Nov 10, 2016, 6:30:03 AM11/10/16
to begi...@perl.org, Jim Gibson
#!/usr/bin/perl
use strict;
use File::Basename;
use utf8;

sub escMe($) {
my $f1 = shift;
my $f2 = '/etc/passwd';
my $f2 = "'/etc/passwd'";
my $f3 = '/etc/passwd';

$f1 = "'" . $f1 . "'";

if($f1 eq $f2) {
print("Files are same $f1$f2\n");
} else {
print("Files are NOT same $f1$f2\n");
}

if (-e $f1)
{
printf "File exist $f1 !\n";
} else {
printf "File not exist $f1 !\n";
}

if (-e '$f2')
{
printf "File exist $f2 !\n";
} else {
printf "File not exist $f2 !\n";
}

if (-e '$f3')
{
printf "File exist $f2 !\n";
} else {
printf "File not exist $f2 !\n";
}


if (-e '/etc/passwd')
{
printf "File exist $f2 !\n";
} else {
printf "File not exist $f2 !\n";
}

return $f1;
}

escMe("/etc/passwd");



Output:

lab# perl test3.pl
Files are same '/etc/passwd''/etc/passwd'
File not exist '/etc/passwd' !
File not exist '/etc/passwd' !
File not exist '/etc/passwd' !
File exist '/etc/passwd' !
0 new messages