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 <jimsgib...@gmail.com> napisał:
> On Nov 10, 2016, at 2:30 AM, jaceke <jac...@op.pl> wrote:
> > 
> > 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.
> 
> 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-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 
> 




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to