CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
printing rwx files

 



zapa99
New User

Mar 19, 2010, 2:08 AM

Post #1 of 7 (730 views)
printing rwx files Can't Post

WHY does he following code block not print readable,writable, or executable files and it always comes back with no output? How can I make it work?

$dir_to_process = $ARGV[0];
opendir DH, $dir_to_process or die "Cannot open $dir_to_process: $!";

foreach $file (readdir DH)
{
#print "$file \n";
"file $file is readable" if -r $file;
"file $file is writable" if -w $file;
"file $file is executable" if -x $file;
}
closedir DH;


7stud
Enthusiast

Mar 19, 2010, 2:52 AM

Post #2 of 7 (728 views)
Re: [zapa99] printing rwx files [In reply to] Can't Post

There is no print() statement anywhere in your code. The double quote operator "" creates and returns a string. You tell perl to create a string if the file is executable, etc. But you don't do anything with the strings that the double quote operator returns, like assign them to variables or print() them, so perl discards the strings. Your question is similar to asking why the following program doesn't print anything:


Code
use strict; 
use warnings;
use 5.010;

"hello";
"world";
"goodbye";



(This post was edited by 7stud on Mar 19, 2010, 3:01 AM)


zapa99
New User

Mar 19, 2010, 11:56 AM

Post #3 of 7 (710 views)
Re: [7stud] printing rwx files [In reply to] Can't Post

Thank you I appreciate the quick response and think I will enjoy it here! I corrected the problem from my original post to include a print statement but now the code below works but NEVER seems to catch a readable/writable/or executable file even when I know there is one (is there something wrong with the 'when' statements)?

for( @ARGV)
{
#print "$file \n";
say "$_ ";
when( -R _ ) {say "is readable"; continue}
when( -w _ ) {say "is writable"; continue}
when( -x _ ) {say "is executable"; continue}

}


7stud
Enthusiast

Mar 19, 2010, 3:31 PM

Post #4 of 7 (699 views)
Re: [zapa99] printing rwx files [In reply to] Can't Post

_ uses the information from the last file test. When perl executes the following line, what is the last file test:


Code
 
when( -R _ ) {say "is readable"; continue}



(This post was edited by 7stud on Mar 19, 2010, 3:40 PM)


zapa99
New User

Mar 19, 2010, 6:02 PM

Post #5 of 7 (691 views)
Re: [7stud] printing rwx files [In reply to] Can't Post

Thanks! I see there is no last file test the way I wrote it so it stays empty. Ok almost got it. So now I add the line:

when( ! -e) {say "File does not exist"}

and it works! I know that ! -e means files does not exist but why will this not work:

when ! $_ -e) {say "File does not exist"}

After all is $_ is the argument from the loop each time?

In Reply To


7stud
Enthusiast

Mar 19, 2010, 10:00 PM

Post #6 of 7 (684 views)
Re: [zapa99] printing rwx files [In reply to] Can't Post

Well, let's see. You're missing an opening parenthesis for the when(). Next, look at all your previous posts and determine where the filename goes in relation to the file test. Does the filename go on the left of the file test?

At this point, your programming is incredibly sloppy. Computer programming does not allow sloppy work. You must precisely write every statement. Computer programming is not like creative writing.

When you write a line that you think should work, look at a previous program you wrote that uses the same type of line and that works, or look at an example in a book. Then determine what is different about the line that won't work and the line that did work.

On the other hand, if you are dyslexic you have no shot at ever learning how to write computer programs.


Quote
Ok almost got it. So now I add the line:

This is the way it works: you do a file test on a filename, or a variable that has been assigned a filename, like $_. Then any other file tests can be performed on _ to speed things up. It's simple and easy, but if that is too confusing for you, forget you ever read about _. At this point, you have much greater concerns to worry about than making your programs more efficient--like getting them to run at all.

Lastly, search google for 'code tags'. Read the first 10 hits, then come back and post what you have learned.


(This post was edited by 7stud on Mar 19, 2010, 10:17 PM)


zapa99
New User

Mar 20, 2010, 12:07 PM

Post #7 of 7 (660 views)
Re: [7stud] printing rwx files [In reply to] Can't Post

Ok look I did not paste the missing ( so that was a copy and paste error I should have checked so I can see how that looked sloppy and can see why there is some flaming for that (although a bit much). I will double check my posts next time but please keep in mind I am a beginner and there is a lot I research myself which I do not post. Code tags in short can help make code more readable and I will try to use it for any larger code segments I post.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives