Re: File test operator (race conditions)

2011-11-27 Thread Jim Gibson

At 5:21 AM +0100 11/28/11, timothy adigun wrote:

 >

 > From: Owen 
 >
 > There is no race condition.
 >
 > And that code demo is correct



Actually, there is in a multitasking environment. Please, check subtitle
"File Locking" in perldoc perlopentut.

   masayoshi  wrote:


 Before calling print method, the file might be deleted by another process
 So I reckoned when "File exists" appeared, the file might be deleted. >_>



I think what you want will be * flock * function. Check subtitle "File
Locking
under perldoc perlopentut,
Also check perldoc -f flock.


There is no race condition in the code that was posted originally 
(posters should remember to include the code on which they are 
commenting -- not everybody will be able to view the original 
message), because there is nothing done after the existence of the 
file is tested. The file exists or does not exist at the time the 
test is made, and the printed output is true for that time (and that 
time only).


A race condition would exist if the program assumed that the file's 
existence did not change after it was tested and acted upon that 
assumption unconditionally. For example, in the following:


if( -e $file ) {
  open(my $fh, '<', $file);
}else{
  open(my $fh,'>',$file);
}

two race conditions can occur: an existing file could be deleted by 
another process between the '-e $file' test and the open-for-reading, 
and a non-existent file could be created between the '-e' test and 
the open-for-writing.


That being the case, you should always test the return value of open 
if you care about whether a file exists or not. Since the '-e' test 
is redundant and possibly misleading, you may as well skip it if the 
existence of the file is a critical decision. If you need to guard 
against simultaneous access to the same file by two processes, then 
some file locking should be considered.


In most cases, there is little chance that another process will 
create or delete a file between two successive Perl statements. Only 
someone who is familiar with the specific situation can determine if 
that improbable event, which may occur eventually, is worth guarding 
against.


--
Jim Gibson
j...@gibson.org

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




Re: File test operator (race conditions)

2011-11-27 Thread timothy adigun
>
> > From: Owen 
> >
> > There is no race condition.
> >
> > And that code demo is correct
>

Actually, there is in a multitasking environment. Please, check subtitle
"File Locking" in perldoc perlopentut.

   masayoshi  wrote:

> Before calling print method, the file might be deleted by another process
> So I reckoned when "File exists" appeared, the file might be deleted. >_>
>
>
I think what you want will be * flock * function. Check subtitle "File
Locking
under perldoc perlopentut,
Also check perldoc -f flock.


-- 
Tim


Re: File test operator (race conditions)

2011-11-27 Thread masayoshi
- Original Message -

> From: Owen 
> 
> There is no race condition.
> 
> And that code demo is correct
>

Before calling print method, the file might be deleted by another process
So I reckoned when "File exists" appeared, the file might be deleted. >_>
 

---

masayoshi & Ayumi Kinoshita
http://tinyurl.com/63zg3op


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




Re: File test operator (race conditions)

2011-11-27 Thread Owen

> Hi,I am masayoshi.
> I read the following article.
>
> http://perltraining.com.au/tips/2005-11-24.html
>
>
> A lot of website use the following script to explain file test
> operators,
> But I reckon I should not write it for race conditions.
> Is this right?
> Thanks in advance.
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $file = "sample.txt";
>
> if ( -e $file ) {
>     print "File exists.\n";
> } else {
>     print "File does not exist.\n";
> }
>
> __END__




There is no race condition.

And that code demo is correct



 --
Owen


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




File test operator (race conditions)

2011-11-27 Thread masayoshi
Hi,I am masayoshi.
I read the following article.

http://perltraining.com.au/tips/2005-11-24.html


A lot of website use the following script to explain file test operators,
But I reckon I should not write it for race conditions.
Is this right?
Thanks in advance.


#!/usr/bin/perl

use strict;
use warnings;

my $file = "sample.txt";

if ( -e $file ) {
    print "File exists.\n";
} else {
    print "File does not exist.\n";
}

__END__

 
---
masayoshi & Ayumi Kinoshita
http://tinyurl.com/63zg3op

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




Re: File test failed!

2011-09-16 Thread Magnus Woldrich

On 2011-09-16 17:53, y...@eisoo.com wrote:

   I want to test if a file exists or it is a link or not


You test for a regular file with -f.
You test for existance with -e.
You test for symbolic links with -l.

See
  perldoc -f -x
for more information.

--
│ Magnus Woldrich
│ m...@japh.se
│ http://japh.se

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




Re: File test failed!

2011-09-16 Thread Shlomi Fish
Hi YYQ,

On Fri, 16 Sep 2011 17:53:25 +0800
"y...@eisoo.com"  wrote:

> Hi, All:
> 
>  I want to test if a file exists or it is a link or not, the code is:
> #!/usr/bin/perl -w
> 
> use strict;
> use File::Basename;
> use File::stat;
> use Fcntl ':mode';
> 
> my $elfobj = $ARGV[0];
> 
> sub strtrim ()
> {
>  #.
> }
> 
> sub copyfile ()
> {
>  #.
> }
> 
> my @deps = `ldd $elfobj`;
> foreach my $lib (@deps) {
>  chomp ($lib);
>  my ($dep,$path) = split ("=>", $lib);
>  ##handle path, and it value is /lib64/ld-linux-x86-64.so.2, which 
> is a link file.
>  if (-e $path) {
>  print $path."\n";
>  }
> }

Well, one problem I can see is that the ldd output contains whitespace before
and after the "=>", so this may trip you. To be sure what exactly is happening,
you can use the perl debugger ("perl -d") to investigate:

http://perl-begin.org/topics/debugging/

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

In Soviet Russia, every time you kill a kitten, God masturbates.
— http://linux.slashdot.org/comments.pl?sid=195378&cid=16009070

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




File test failed!

2011-09-16 Thread y...@eisoo.com

Hi, All:

I want to test if a file exists or it is a link or not, the code is:
#!/usr/bin/perl -w

use strict;
use File::Basename;
use File::stat;
use Fcntl ':mode';

my $elfobj = $ARGV[0];

sub strtrim ()
{
#.
}

sub copyfile ()
{
#.
}

my @deps = `ldd $elfobj`;
foreach my $lib (@deps) {
chomp ($lib);
my ($dep,$path) = split ("=>", $lib);
##handle path, and it value is /lib64/ld-linux-x86-64.so.2, which 
is a link file.

if (-e $path) {
print $path."\n";
}
}

But the highlighted code always return undef. When put the code outside 
of the foreach, it ok. Any one encountered this situation?


Re: Inconsistent results from file test (-X) operators

2007-05-16 Thread John W. Krahn
R (Chandra) Chandrasekhar wrote:
> Dear Folks,

Hello,

> I have encountered inconsistent behaviour with the file test (-X)
> operators. I am using perl, v5.8.8 built for i486-linux-gnu-thread-multi
> on a Kubuntu Feisty system.
> 
> I show below my minimal test file:
> 
> --- Minimal Test File ---
> #!/usr/bin/perl -w

use strict;

> use diagnostics;
> 
> # Minimal example for problem with file test, or -X, operators
> 
> $testdir = "/usr";
> 
> opendir(DH, $testdir) or die "Could not open $testdir: $!\n";
> 
> while (defined($file = readdir(DH)))
> {
> if (-d $file)

perldoc -f readdir
readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir".  If used in list context, returns all the rest of the
entries in the directory.  If there are no more entries, returns
an undefined value in scalar context or a null list in list
context.

If you’re planning to filetest the return values out of a
"readdir", you’d better prepend the directory in question.
Otherwise, because we didn’t "chdir" there, it would have been
testing the wrong file.

opendir(DIR, $some_dir) || die "can’t opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;


So:

if (-d "$testdir/$file")


> {
> print "$file is a directory\n";
> }
> elsif (-f $file)

And:

elsif (-f "$testdir/$file")

Or more simply:

elsif (-f _)


> {
> print "$file is a regular file\n";
> }
> else
> {
> print "$file is neither a directory nor a regular file\n";
> }
>}
> 
> closedir(DH);



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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Inconsistent results from file test (-X) operators

2007-05-16 Thread Chas Owens

On 5/16/07, R (Chandra) Chandrasekhar <[EMAIL PROTECTED]> wrote:
snip

Can anyone please tell me why I get this inconsistent behaviour and how to
overcome it?

snip

It is not inconsistent, you are using file names without the proper
path.  If a file or directory exists in the current directory with the
same name as one in /usr you will get a result, otherwise there is no
file, so you are told false for both.  You can fix your code by either
prefixing the file name with the directory or doing chdir() into
$testdir.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Inconsistent results from file test (-X) operators

2007-05-16 Thread R (Chandra) Chandrasekhar

Dear Folks,

I have encountered inconsistent behaviour with the file test (-X) operators. I 
am using perl, v5.8.8 built for i486-linux-gnu-thread-multi on a Kubuntu Feisty 
system.


I show below my minimal test file:

--- Minimal Test File ---
#!/usr/bin/perl -w
use diagnostics;

# Minimal example for problem with file test, or -X, operators

$testdir = "/usr";

opendir(DH, $testdir) or die "Could not open $testdir: $!\n";

while (defined($file = readdir(DH)))
{
if (-d $file)
{
print "$file is a directory\n";
}
elsif (-f $file)
{
print "$file is a regular file\n";
}
else
{
print "$file is neither a directory nor a regular file\n";
}
   }

closedir(DH);
-

The results I get are:

-
include is a directory
src is a directory
lib is a directory
sbin is neither a directory nor a regular file
bin is a directory
local is neither a directory nor a regular file
lib32 is neither a directory nor a regular file
games is neither a directory nor a regular file
share is neither a directory nor a regular file
. is a directory
.. is a directory
lib64 is neither a directory nor a regular file
X11R6 is neither a directory nor a regular file
-

and

ls -al /usr

gives:

-
drwxr-xr-x  13 root root  4096 2007-05-15 16:18 .
drwxr-xr-x  23 root root  4096 2007-05-15 00:55 ..
drwxr-xr-x   2 root root 69632 2007-05-17 00:12 bin
drwxr-xr-x   2 root root  4096 2007-05-15 08:45 games
drwxr-xr-x  35 root root  4096 2007-05-15 16:10 include
drwxr-xr-x 149 root root 69632 2007-05-16 14:33 lib
drwxr-xr-x   3 root root  4096 2007-05-15 01:33 lib32
drwxr-xr-x   3 root root  4096 2007-05-15 16:18 lib64
drwxr-xr-x  10 root root  4096 2007-04-17 13:19 local
drwxr-xr-x   2 root root 12288 2007-05-17 00:12 sbin
drwxr-xr-x 244 root root 12288 2007-05-16 13:55 share
drwxrwsr-x   4 root src   4096 2007-04-17 13:23 src
drwxr-xr-x   3 root root  4096 2007-04-17 13:21 X11R6
-

Can anyone please tell me why I get this inconsistent behaviour and how to 
overcome it?


I have read of the possibility of a race condition with these operators but I am 
unsure whether it applies here.


Thank you.

Chandra
17 May 07

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




File test operator -C in Win32

2006-07-08 Thread Monomachus
What does really means -C file test operator on Win32 platform? Creation time 
or what? 
==
Thanks,
   Monomachus




-
This e-mail was sent using Mail.md




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Fw: File test operator -C in Win32

2006-07-08 Thread Monomachus
--- Forwarded message --
From: Monomachus <[EMAIL PROTECTED]>
To:  
Date: 08.07.06 18:12
Subject: File test operator -C in Win32

What does really means -C file test operator on Win32 platform? Creation time 
or what? 
==
Thanks,
   Monomachus
--- End of forwarded message --


-- 
C uvazheniem,
Monomachus 

Don't worry, Be Happy


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




File test operator -C in Win32

2006-07-08 Thread Monomachus
What does really means -C file test operator on Win32 platform? Creation time 
or what? 
==
Thanks,
   Monomachus

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: file test

2006-01-24 Thread Adriano Rodrigues Ferreira



The operator -e (like the other -X operators) takes a filename or file
handle, and not a glob or file name pattern with wildcards. As others
pointed, you use the builtin glob to do the expansion (either explicitly
or via <>).

Then you might say:

for (glob 'file*') {
   print "true\n" if -e $_;
}

Note that if the glob was expanded, it is generally obvious that the file
exists (that is, -e $_ above will be true above). But if no filename
matching 'file*' was found, the function return 'file*' unchanged.

Regards,
Adriano.

--
We don't need no stinking Java - we want to get some fun.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: file test

2006-01-24 Thread Adriano Rodrigues Ferreira


The operator -e (like the other -X operators) takes a filename or file
handle, and not a glob or file name pattern with wildcards. As others
pointed, you use the builtin glob to do the expansion (either explicitly
or via <>).

Then you might say:

for (glob 'file*') {
   print "true\n" if -e $_;
}

Note that if the glob was expanded, it is generally obvious that the file
exists (that is, -e $_ above will be true above). But if no filename
matching 'file*' was found, the function return 'file*' unchanged.

Regards,
Adriano.

--
dear all,

i am trying to check if there are any files named file* (e.g.
file_001.txt file1.doc) in my directory.

if( -e "file*" ) { print("true\n"); } # this doesn't work
else { print("false\n"); }

if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
else { print("false\n"); }

thank you in advance, any answer would be greatly appreciated.

-H.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: file test

2006-01-24 Thread Bob Showalter

hien wrote:

dear all,

i am trying to check if there are any files named file* (e.g.  
file_001.txt file1.doc) in my directory.


if( -e "file*" ) { print("true\n"); } # this doesn't work
else { print("false\n"); }

if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
else { print("false\n"); }


Ouch, there's the dreaded "doesn't work"! (c.f. 
http://perl.plover.com/Questions4.html) Fortunately, you told us what 
you are after, so the question can be answered.


-e only takes a single filename.

You need a glob:

   print "Found!" if glob('file*');

Or the slightly deprecated:

   print "Found!" if ;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: file test

2006-01-24 Thread Gerard Robin
On Mon, Jan 23, 2006 at 09:47:40PM +0100, hien wrote:

> i am trying to check if there are any files named file* (e.g.  
> file_001.txt file1.doc) in my directory.

#!/usr/bin/perl

use warnings;
use strict;

my $i = 0;
while () {

  if (-e $_) {
print "$_ exists\n";
$i++;
}
}
print "there are $i files\n";

hth
-- 
Gérard


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: file test

2006-01-23 Thread Chris Devers
On Mon, 23 Jan 2006, hien wrote:

> i am trying to check if there are any files named file* (e.g. 
> file_001.txt file1.doc) in my directory.

perldoc -f glob


-- 
Chris Devers
DO NOT LEAVE IT IS NOT REAL

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




file test

2006-01-23 Thread hien

dear all,

i am trying to check if there are any files named file* (e.g.  
file_001.txt file1.doc) in my directory.


if( -e "file*" ) { print("true\n"); } # this doesn't work
else { print("false\n"); }

if( -e "m/^file/" ) { print("true\n"); } # this doesn't work either
else { print("false\n"); }

thank you in advance, any answer would be greatly appreciated.

-H.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: File Test Question

2005-11-30 Thread jm
according to Programming Perl (p. 98)...

-w = file is writable by effective uid/gid
-s = file has nonzero size (returns size)

-w only tells if the file's permissions allow it to be written to, has
nothing to do with whether or not it already has data.

save the return value of -s and check that value to see if the file
has data or not


On 11/30/05, Dave Adams <[EMAIL PROTECTED]> wrote:
> *My Code:*
>
> my $logfile = "logfile_with_content";
> if (-w $logfile) {
> print ("True - file exists but empty");
> }
> if (-s $logfile) {
> print ("True - file exist and has content");
> }
>
> *My Output:*
>
> True - file exists but empty True - file exist and has content
>
> *My Question:*
>
> Why do both test evaluate to true when the file called
> "logfile_with_content" is 5K in size?  I would expect the second file test
> to only work?  Any advice?
>
> Thanks
>
>


--
since this is a gmail account, please verify the mailing list is
included in the reply to addresses

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: File Test Question

2005-11-30 Thread Timothy Johnson

I don't think the -x tests mean what you think they mean.

>From "perldoc -f -x":

   -r  File is readable by effective uid/gid.
   -w  File is writable by effective uid/gid.
   -x  File is executable by effective uid/gid.
   -o  File is owned by effective uid.

   -R  File is readable by real uid/gid.
   -W  File is writable by real uid/gid.
   -X  File is executable by real uid/gid.
   -O  File is owned by real uid.

   -e  File exists.
   -z  File has zero size (is empty).
   -s  File has nonzero size (returns size in bytes).

   -f  File is a plain file.
   -d  File is a directory.
   -l  File is a symbolic link.
   -p  File is a named pipe (FIFO), or Filehandle is a pipe.
   -S  File is a socket.
   -b  File is a block special file.
   -c  File is a character special file.
   -t  Filehandle is opened to a tty.

   -u  File has setuid bit set.
   -g  File has setgid bit set.
   -k  File has sticky bit set.

   -T  File is an ASCII text file (heuristic guess).
   -B  File is a "binary" file (opposite of -T).

   -M  Script start time minus file modification time, in days.
   -A  Same for access time.
   -C  Same for inode change time (Unix, may differ for other
platforms)




-Original Message-
From: Dave Adams [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 30, 2005 11:16 AM
To: beginners perl
Subject: File Test Question

*My Code:*

my $logfile = "logfile_with_content";
if (-w $logfile) {
print ("True - file exists but empty");
}
if (-s $logfile) {
print ("True - file exist and has content");
}

*My Output:*

True - file exists but empty True - file exist and has content

*My Question:*

Why do both test evaluate to true when the file called
"logfile_with_content" is 5K in size?  I would expect the second file
test
to only work?  Any advice?

Thanks


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: File Test Question

2005-11-30 Thread Wiggins d'Anconia
Dave Adams wrote:
> *My Code:*
> 
> my $logfile = "logfile_with_content";
> if (-w $logfile) {
> print ("True - file exists but empty");
> }
> if (-s $logfile) {
> print ("True - file exist and has content");
> }
> 
> *My Output:*
> 
> True - file exists but empty True - file exist and has content
> 
> *My Question:*
> 
> Why do both test evaluate to true when the file called
> "logfile_with_content" is 5K in size?  I would expect the second file test
> to only work?  Any advice?
>

-w tests whether the file is writable by the current effective UID/GID,
it has nothing to do with the contents of the file.

perldoc -f -e

For more details.

http://danconia.org

> Thanks
> 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




File Test Question

2005-11-30 Thread Dave Adams
*My Code:*

my $logfile = "logfile_with_content";
if (-w $logfile) {
print ("True - file exists but empty");
}
if (-s $logfile) {
print ("True - file exist and has content");
}

*My Output:*

True - file exists but empty True - file exist and has content

*My Question:*

Why do both test evaluate to true when the file called
"logfile_with_content" is 5K in size?  I would expect the second file test
to only work?  Any advice?

Thanks


Re: file test doesn't seem to be working

2002-02-05 Thread John Mooney


>>> "Brett W. McCoy" <[EMAIL PROTECTED]> 2/5/2002 10:35:48 AM >>>
>
>That's just more a matter of style -- I get the willies when subroutines
>modify global values invisibly.


Great advice. thanks


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-05 Thread Brett W. McCoy

On Tue, 5 Feb 2002, John Mooney wrote:

> Actually, it is not an incorrect way I believe, so much as he is using
> slightly incorrect syntax.  From the Nutshell ...

I really meant 'incorrect' in its usage, not the syntax -- he was using it
in a void context.

> ... I think the key is that he was not using a token that resulted in an
> explicit evaluation, returning & capturing a result. By wrapping the 2nd
> and 3rd tokens of the ternary clause in parens, this result can be
> acheived.
>
> Here's an example I use often in CGIs to accomplish background-color
> swapping in HTML table rows - achieiving an old computer-paper,
> green/white effect:
>
> local $swapper = -1;  # globals
> local $bgcolor   = '';
>
> sub swapBGcolor {
>(($swapper *= -1) > 0 ) ? ($bgcolor="#FF") : ($bgcolor="#F5DCC7");
> }

That can be dangerous, IMHO, modifying globals like that (I know it would
cause some problems in mod_perl!)  I would instead code it so that the
little subroutine returns the value you need:

$bgcolor = swapBGcolor();

sub swapBGcolor {

return ($swapper *= -1) > 0 ? '#FF' : '#F5DCC7'
}

That's just more a matter of style -- I get the willies when subroutines
modify global values invisibly.

-- Brett
  http://www.chapelperilous.net/

The English have no respect for their language, and will not teach
their children to speak it.
-- G. B. Shaw


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-05 Thread John Mooney

>>> "Brett W. McCoy" <[EMAIL PROTECTED]> 2/4/2002 9:28:42 PM >>>
>>On Mon, 4 Feb 2002, david wright wrote:
>>
>> i can't use the ternary operator like this? (damn waste if not) thanks.
>>
>> foreach $dup (@array){
>> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
>> )
>
>Yes, that is an incorrect way to use the ?: operator -- the operataor is
>handed an expression that yields a boolean value, and it returns a value
>based on what that boolean value is:
>
> Here is a one liner that accomplishes what you want:
>
>$ perl -e '$ans = (-d "/usr/bin") ? "yes\n" : "no\n"; print $ans'
>yes
>

Actually, it is not an incorrect way I believe, so much as he is using slightly 
incorrect syntax.  From the Nutshell ...


Ternary ?: is the conditional operator. It works much like an if-then-else statement, 
but it can safely be embedded within other operations and functions. 

test_expr ? if_true_expr : if_false_expr
If the test_expr is true, only the if_true_expr is evaluated. Otherwise, only the 
if_false_expr is evaluated. Either way, the value of the  _evaluated_expression_  
becomes the value of the entire expression.


 I think the key is that he was not using a token that resulted in an explicit 
evaluation, returning & capturing a result. By wrapping the 2nd and 3rd tokens of the 
ternary clause in parens, this result can be acheived.

Here's an example I use often in CGIs to accomplish background-color swapping in HTML 
table rows - achieiving an old computer-paper, green/white effect:

local $swapper = -1;  # globals
local $bgcolor   = '';   

sub swapBGcolor {
   (($swapper *= -1) > 0 ) ? ($bgcolor="#FF") : ($bgcolor="#F5DCC7");
}

Each time swapBGcolor is invoked, the value of $swapper is multiplied by -1, flipping 
between +1 or -1.  When that var is positive, the TRUE expression of the ternary 
opertaor is 'evaluated', assigning a value to $bgcolor at the same time. When $swapper 
is negative (false), the 2nd value of the global $bgcolor is assigned during the 
'evaluation'.

I remember struggling with getting this to work and finally hitting upon using parens 
to capture the result of an expression and returning it to the ? operator's result.

FYI
- John




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Jeff 'japhy' Pinyan

On Feb 4, Brett W. McCoy said:

>On Mon, 4 Feb 2002, david wright wrote:
>
>> i can't use the ternary operator like this? (damn waste if not) thanks.
>>
>> foreach $dup (@array){
>> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
>> )
>
>Yes, that is an incorrect way to use the ?: operator -- the operataor is
>handed an expression that yields a boolean value, and it returns a value
>based on what that boolean value is:

That's not to say his use is incorrect.

  EXPR ? EXPR : EXPR

His code fits...

  (-d $dup) ? (print "yes: $dup\n") : (print "no:  $dup\n")

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Brett W. McCoy

On Mon, 4 Feb 2002, david wright wrote:

> i can't use the ternary operator like this? (damn waste if not) thanks.
>
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> )

Yes, that is an incorrect way to use the ?: operator -- the operataor is
handed an expression that yields a boolean value, and it returns a value
based on what that boolean value is:

 Here is a one liner that accomplishes what you want:

$ perl -e '$ans = (-d "/usr/bin") ? "yes\n" : "no\n"; print $ans'
yes

-- Brett
  http://www.chapelperilous.net/

"The Lord gave us farmers two strong hands so we could grab as much as
we could with both of them."
-- Joseph Heller, "Catch-22"


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread John W. Krahn

David Wright wrote:
> 
> i am sending @array a directory (i.e. example /usr/dw5) which contains a
> lot of files and folders (directories).
> (i have already checked the value of @array and $dup and they are as
> desired.) What i want to accomplish is: print yes, "$dup (file name)" if
> it's a directory (folder) i am testing it on a directory which is
> composed of 90% folders. The example below will only print yes for .
> and .. and "no" for everything else. I have tried it with other file
> test's (i.e. -f, -A, -e, etc,...) none of them work as desired,... maybe
> i can't use the ternary operator like this? (damn waste if not) thanks.
> 
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> )


This works fine on my Linux system.  BTW this is usually written as:

print -d $dup ? 'yes: ' : 'no: ', "$dup\n";


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread david wright


On Monday, February 4, 2002, at 07:26 PM, Jeff 'japhy' Pinyan wrote:

> On Feb 4, david wright said:
>
>> i am sending @array a directory (i.e. example /usr/dw5) which 
>> contains a
>> lot of files and folders (directories).
>
> Be warned that readdir() returns NAMES, not PATHS.
>
>   opendir DIR, "/some/dir";
>   @files = readdir DIR;
>   closedir DIR;
>
> @files will contain (".", "..", "file1", "blah", ...).  Notice how those
> names do NOT have "/some/dir/" at the beginning?
>
>   for (@files) {
> print "$_ is ok\n" if -d "/some/dir/$_";
>   }
>
> --
> Jeff "japhy" Pinyan  [EMAIL PROTECTED]  
> http://www.pobox.com/~japhy/
> RPI Acacia brother #734   http://www.perlmonks.org/   
> http://www.cpan.org/
> ** Look for "Regular Expressions in Perl" published by Manning, in 
> 2002 **
>  what does y/// stand for?   why, yansliterate of 
> course.
>
>

Damn it, now it makes sense, exactly,... it's just reading a "string" it 
doesn't know what it's representing, that's why i have to chdir.   
(Guess that answers my question of my i would have to chdir if i can 
readdir ;-) ) excellent!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread Jeff 'japhy' Pinyan

On Feb 4, david wright said:

>i am sending @array a directory (i.e. example /usr/dw5) which contains a 
>lot of files and folders (directories).

Be warned that readdir() returns NAMES, not PATHS.

  opendir DIR, "/some/dir";
  @files = readdir DIR;
  closedir DIR;

@files will contain (".", "..", "file1", "blah", ...).  Notice how those
names do NOT have "/some/dir/" at the beginning?

  for (@files) {
print "$_ is ok\n" if -d "/some/dir/$_";
  }

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test doesn't seem to be working

2002-02-04 Thread david wright


On Monday, February 4, 2002, at 06:57 PM, Timothy Johnson wrote:

>
> Did you chdir to the directory?  Otherwise your script might be 
> checking the
> wrong directory.
>
> opendir(DIR,"/mydir");
> @array = readdir(DIR);
> chdir "/mydir";
> foreach $dup (@array){
> (-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
> }
>
>
well the return value of @array prints all the files in the desired 
directory, doesn't that mean that is the directory that the loop is 
operating on and -d is checking?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: file test doesn't seem to be working

2002-02-04 Thread Timothy Johnson


Did you chdir to the directory?  Otherwise your script might be checking the
wrong directory.

opendir(DIR,"/mydir");
@array = readdir(DIR);
chdir "/mydir";
foreach $dup (@array){
(-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
}

-Original Message-
From: david wright [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 04, 2002 5:51 PM
To: [EMAIL PROTECTED]
Subject: file test doesn't seem to be working


i am sending @array a directory (i.e. example /usr/dw5) which contains a 
lot of files and folders (directories).
(i have already checked the value of @array and $dup and they are as 
desired.) What i want to accomplish is: print yes, "$dup (file name)" if 
it's a directory (folder) i am testing it on a directory which is 
composed of 90% folders. The example below will only print yes for . 
and .. and "no" for everything else. I have tried it with other file 
test's (i.e. -f, -A, -e, etc,...) none of them work as desired,... maybe 
i can't use the ternary operator like this? (damn waste if not) thanks.

foreach $dup (@array){
(-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
)


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




file test doesn't seem to be working

2002-02-04 Thread david wright

i am sending @array a directory (i.e. example /usr/dw5) which contains a 
lot of files and folders (directories).
(i have already checked the value of @array and $dup and they are as 
desired.) What i want to accomplish is: print yes, "$dup (file name)" if 
it's a directory (folder) i am testing it on a directory which is 
composed of 90% folders. The example below will only print yes for . 
and .. and "no" for everything else. I have tried it with other file 
test's (i.e. -f, -A, -e, etc,...) none of them work as desired,... maybe 
i can't use the ternary operator like this? (damn waste if not) thanks.

foreach $dup (@array){
(-d  $dup) ? print "yes: $dup  \n": print "no:  $dup \n";
)


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test

2001-11-30 Thread Luke Bakken

Actually `dir /B` won't produce anything since dir isn't a real coomand -
it's a shell builtin.

This may help:

c:\>perl -e"for (<*>) { print $_, qq(\n) if(! -x $_ && -B $_ ) }"

Luke

On Thu, 29 Nov 2001, Carl Rogers wrote:

> At 08:09 PM 11/29/2001 +, [EMAIL PROTECTED] wrote:
> >How can I do more then one file test for file?
> >
> >I'm trying to test a list of files to see if they're a binary file, that
> >isn't executable.
>
> If I understand correctly, you want to check all files in a certain
> directory to see if they AREN'T executables?
> In WIN NT:
>
> foreach (`dir /B`)  # gives bare format of the files in the
> working directory
> {
>  if ($_ !~ m/\.exe/)
>  {
>  ## do whatever you want to the files that aren't .exe
>  }
>  else
>  {
>
>  }
>
> }
> Hope this helps
> Carl
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test

2001-11-29 Thread Brett W. McCoy

On Thu, 29 Nov 2001, Carl Rogers wrote:

> At 08:09 PM 11/29/2001 +, [EMAIL PROTECTED] wrote:
> >How can I do more then one file test for file?
> >
> >I'm trying to test a list of files to see if they're a binary file, that
> >isn't executable.
>
> If I understand correctly, you want to check all files in a certain
> directory to see if they AREN'T executables?
> In WIN NT:

The canonical (and portable) way of doing this in Perl is to use the -X
operators

-B determines if the file is binary
-x determines if the file is executable

so, for each file you could do

if(-B "$file" && ! -x "$file" ) {

#do stuff
}

I believe these operators work under Win32 as well as Unix systems.

See perldoc -f -X for more details.

-- Brett
  http://www.chapelperilous.net/

Rotten wood cannot be carved.
-- Confucius, "Analects", Book 5, Ch. 9


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test

2001-11-29 Thread Carl Rogers

At 08:09 PM 11/29/2001 +, [EMAIL PROTECTED] wrote:
>How can I do more then one file test for file?
>
>I'm trying to test a list of files to see if they're a binary file, that 
>isn't executable.

If I understand correctly, you want to check all files in a certain 
directory to see if they AREN'T executables?
In WIN NT:

foreach (`dir /B`)  # gives bare format of the files in the 
working directory
{
 if ($_ !~ m/\.exe/)
 {
 ## do whatever you want to the files that aren't .exe
 }
 else
 {

 }

}
Hope this helps
Carl


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




file test

2001-11-29 Thread srogers

How can I do more then one file test for file?

I'm trying to test a list of files to see if they're a binary file, that isn't 
executable.

-Stephanie

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file test fails 2nd time thru

2001-10-30 Thread Andrea Holstein

Hanson wrote:
> 
> I have a fairly large perl script which optionally runs
> through a few subroutines doing similar things to different
> files. Sorry I can't be more detailed.
> My problem is this: within these subroutines are file tests:
> 
> (-e $File) or die
> 
> type of stuff.
> 
> The first time through these tests work. The next time
> through they fail.
> 
> Does anyone have any ideas why this may be happening?

Sorry, you need to be more detailed.
Show us a bit more code and 
explain better the error occured.

Greetings,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




file test fails 2nd time thru

2001-10-30 Thread HANSON

Hi Gurus!

I have a fairly large perl script which optionally runs 
through a few subroutines doing similar things to different 
files. Sorry I can't be more detailed.
My problem is this: within these subroutines are file tests:

(-e $File) or die

type of stuff.

The first time through these tests work. The next time 
through they fail.

Does anyone have any ideas why this may be happening?

Thanks!

mark

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]