See inline comments below.

On Nov 10, 2016, at 3:15 AM, jaceke <jac...@op.pl> wrote:
> 
> This example will be better :
> 
> #!/usr/bin/perl
> use strict;
> use File::Basename;
> use utf8;
> 

You should indent all blocks (subroutines, if statements, loops, etc.) to make 
it easier to read and follow the structure of your program.

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

This line adds apostrophes at the beginning and end of the file path being 
passed into the subroutine. You do NOT want to do that!

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

$f1 contains this:   ‘/etc/passwd’
$f2 contains this:  /etc/passwd

One has single quotes and the other does not. They are not the same.

Let’s use the constructs q() and qq() for better readability. q(abc) is the 
same as ‘abc’, and qq(xyz) is the same as “xyz”. qq() allows interpolation of 
variables and escape sequences, q() does not.

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

The file q(‘/etc/passwd’), which has two single-quote characters, one at the 
beginning and one at the end, does not exist.

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

You are testing for the existence of a file with the name q($f2). In other 
words, the file name (or path) has three characters in it: dollar sign, eff, 
two. That file does not exist (in the current directory).

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

Same problem. q($f3) does not exist.

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

Same problem. q($f4) does not exist.

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

The file q(/etc/passwd) does exist.

> 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 !
> 


--
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