Re: Control-D Bug?
FYI, when you type CTRL-D, the tty driver echos back 4 characters ^D\b \b (where \b is or backspace). So when perl outputs its first line, it writes 3\n (which the tty driver converts to 3\r\n), so the ^ is overwritten by the 3, leaving 3D. Edward Moy Apple On Feb 1, 2006, at 10:01 AM, Jonathan Levi MD wrote: At 4:13 PM +1030 2/1/06, Paul McCann wrote: Jonathan Levi wrote: #!/usr/bin/env perl # [comments omitted] print reverse <>; drj2:learningperl4 jonathan$ p052e1.pl ... 3 3D .. I'm pretty sure that you're just seeing an artefact of executing in the terminal (ie it's a display problem, rather than a problem with the output). If you try to "catch" the output instead, via p052e1.pl > output.txt and then "less output.txt" you should notice that the "^D" terminated input gives the same result as when you point at a file for the input. You were right, the stray "D" didn't appear in output.txt, so I don't have to worry about it creeping into output files. Many thanks, Jonathan
Re: Control-D Bug?
At 4:13 PM +1030 2/1/06, Paul McCann wrote: Jonathan Levi wrote: #!/usr/bin/env perl # [comments omitted] print reverse <>; drj2:learningperl4 jonathan$ p052e1.pl ... 3 3D .. I'm pretty sure that you're just seeing an artefact of executing in the terminal (ie it's a display problem, rather than a problem with the output). If you try to "catch" the output instead, via p052e1.pl > output.txt and then "less output.txt" you should notice that the "^D" terminated input gives the same result as when you point at a file for the input. You were right, the stray "D" didn't appear in output.txt, so I don't have to worry about it creeping into output files. Many thanks, Jonathan
Re: Control-D Bug?
Jonathan Levi wrote: #!/usr/bin/env perl # [comments omitted] print reverse <>; drj2:learningperl4 jonathan$ p052e1.pl 1 2 3 3D 2 1 I'm pretty sure that you're just seeing an artefact of executing in the terminal (ie it's a display problem, rather than a problem with the output). If you try to "catch" the output instead, via p052e1.pl > output.txt and then "less output.txt" you should notice that the "^D" terminated input gives the same result as when you point at a file for the input. Cheers, Paul
Control-D Bug?
Doing an early problem (reading, reversing and printing a list of strings read from input) in the Llama Book, 4th Ed., I wrote: #!/usr/bin/env perl # [comments omitted] print reverse <>; This worked with a separately created input file: drj2:learningperl4 jonathan$ cat someInput.txt 1 2 3 drj2:learningperl4 jonathan$ p052e1.pl someInput.txt 3 2 1 but it didn't work quite that way when I used standard input, terminated with control-D: drj2:learningperl4 jonathan$ p052e1.pl 1 2 3 3D 2 1 Is this supposed to happen? --MacOS X 10.3.9, perl "v5.8.1-RC3 built for darwin-thread-multi-2level (with 1 registered patch, see perl -V for more detail)" tia, Jonathan
Fwd: -d seems to disable rename(old,new)
That was it. Thanks. I had found the example on a website and used their syntax. I'm something of a newbie. I changed it as per your suggestion, and it seems to be working like I had anticipated in the first place. Thanks Begin forwarded message: From: [EMAIL PROTECTED] Date: Tue Nov 26, 2002 1:43:32 PM US/Pacific To: "Matthew Galaher" <[EMAIL PROTECTED]>, [EMAIL PROTECTED] Subject: RE: -d seems to disable rename(old,new) Why open the filehandle then test the filehandle? I believe you should just test the file path with the -d then only open the handle afterwards. http://danconia.org On Tue, 26 Nov 2002 13:10:27 -0800, Matthew Galaher <[EMAIL PROTECTED]> wrote: I've written a Dropscript droplet in perl that is meant to rename files based on a specific naming convention. As a safety measure I wish to check if they drop a folder. I check this using "if(-d FILEHANDLE){" which works, but it also seems to make the "rename" fail. What follows is what I hope is the relevant code from the script. If I comment out the part that checks whether it's a directory, the "rename" function works. Any ideas? Thanks in advance. open(FILEHANDLE,$test_if_dir); if(-d FILEHANDLE){#IF IT'S A FOLDER DON'T RENAME IT $write = "false"; $because_is_dir = $because_is_dir . "Will not rename the following item because it is a folder:\n $full_path_dropped\n\n"; $print_switch = 1;# if there is a warning this sets it to show report. } else{ if (($new_path =~ m/NOT FOUND!/)&&($write eq "true")){#don't enter block if found to be directory $because_something_not_found = $because_something_not_found . "Will not rename the following item because some element not found (job number, client name, message type, etc.):\n$full_path_dropped\n\n"; $write = "false"; $print_switch = 1;# if there is a warning this sets it to show report. } elsif($write eq "false"){#IF parse_path AND check_dir functions set $write to false then: $because_exists = $because_exists . "Will not rename the following item because a file with that name already exists\n$new_path\n$full_path_dropped\n\n"; $print_switch = 1;# if there is a warning this sets it to show report. } elsif ($write eq "true"){ $yes = "true"; #THE PART THAT FAILS: rename($full_path_dropped,$new_path) or print REPORT "\n**FAILD TO RENAME FILE**\n"; $will_rename = $will_rename . "path was \n" . $full_path_dropped . "\nnew path would be \n" . $new_path . "\n\n"; $print_switch = 1;#comment this out when done. This way report won't open if no warnings are written. } else{ $print_switch = 1;# if there is a warning this sets it to show report. } } close(FILEHANDLE,$test_if_dir);
RE: -d seems to disable rename(old,new)
Why open the filehandle then test the filehandle? I believe you should just test the file path with the -d then only open the handle afterwards. http://danconia.org On Tue, 26 Nov 2002 13:10:27 -0800, Matthew Galaher <[EMAIL PROTECTED]> wrote: > I've written a Dropscript droplet in perl that is meant to rename files > based on a specific naming convention. As a safety measure I wish to > check if they drop a folder. I check this using "if(-d FILEHANDLE){" > which works, but it also seems to make the "rename" fail. What follows > is what I hope is the relevant code from the script. If I comment out > the part that checks whether it's a directory, the "rename" function > works. Any ideas? > Thanks in advance. > > > > open(FILEHANDLE,$test_if_dir); > > if(-d FILEHANDLE){#IF IT'S A FOLDER DON'T RENAME IT > $write = "false"; > $because_is_dir = $because_is_dir . "Will not rename the following > item because it is a folder:\n $full_path_dropped\n\n"; > $print_switch = 1;# if there is a warning this sets it to show report. > } > > else{ > if (($new_path =~ m/NOT FOUND!/)&&($write eq "true")){#don't enter > block if found to be directory > $because_something_not_found = $because_something_not_found . >"Will > not rename the following item because some element not found (job > number, client name, message type, etc.):\n$full_path_dropped\n\n"; > $write = "false"; > $print_switch = 1;# if there is a warning this sets it to show > report. > } > > elsif($write eq "false"){#IF parse_path AND check_dir functions > set $write to false then: > $because_exists = $because_exists . "Will not rename the >following > item because a file with that name already > exists\n$new_path\n$full_path_dropped\n\n"; > $print_switch = 1;# if there is a warning this sets it to show > report. > } > elsif ($write eq "true"){ > $yes = "true"; > #THE PART THAT FAILS: > rename($full_path_dropped,$new_path) or print REPORT >"\n**FAILD TO > RENAME FILE**\n"; > $will_rename = $will_rename . "path was \n" . >$full_path_dropped . > "\nnew path would be \n" . $new_path . "\n\n"; > $print_switch = 1;#comment this out when done. This way report >won't > open if no warnings are written. > } > > else{ > $print_switch = 1;# if there is a warning this sets it to show > report. > } > } > close(FILEHANDLE,$test_if_dir); >
-d seems to disable rename(old,new)
I've written a Dropscript droplet in perl that is meant to rename files based on a specific naming convention. As a safety measure I wish to check if they drop a folder. I check this using "if(-d FILEHANDLE){" which works, but it also seems to make the "rename" fail. What follows is what I hope is the relevant code from the script. If I comment out the part that checks whether it's a directory, the "rename" function works. Any ideas? Thanks in advance. open(FILEHANDLE,$test_if_dir); if(-d FILEHANDLE){#IF IT'S A FOLDER DON'T RENAME IT $write = "false"; $because_is_dir = $because_is_dir . "Will not rename the following item because it is a folder:\n $full_path_dropped\n\n"; $print_switch = 1;# if there is a warning this sets it to show report. } else{ if (($new_path =~ m/NOT FOUND!/)&&($write eq "true")){#don't enter block if found to be directory $because_something_not_found = $because_something_not_found . "Will not rename the following item because some element not found (job number, client name, message type, etc.):\n$full_path_dropped\n\n"; $write = "false"; $print_switch = 1;# if there is a warning this sets it to show report. } elsif($write eq "false"){#IF parse_path AND check_dir functions set $write to false then: $because_exists = $because_exists . "Will not rename the following item because a file with that name already exists\n$new_path\n$full_path_dropped\n\n"; $print_switch = 1;# if there is a warning this sets it to show report. } elsif ($write eq "true"){ $yes = "true"; #THE PART THAT FAILS: rename($full_path_dropped,$new_path) or print REPORT "\n**FAILD TO RENAME FILE**\n"; $will_rename = $will_rename . "path was \n" . $full_path_dropped . "\nnew path would be \n" . $new_path . "\n\n"; $print_switch = 1;#comment this out when done. This way report won't open if no warnings are written. } else{ $print_switch = 1;# if there is a warning this sets it to show report. } } close(FILEHANDLE,$test_if_dir);
Re: -d
At 8:47 am -0700 2/11/02, Dave Gomez wrote: here is a code example using find:file, note it is recursive, and will work with where you are sitting, or a passed argument of start point including ~ Dave Gomez #!/usr/bin/perl -lw # fdirs - find all directories @ARGV = qw(.) unless @ARGV; use File::Find (); sub find(&@) { &File::Find::find } *name = *File::Find::name; find { print $name if -d } @ARGV; Thanks everyone for your help. As to the above script, it gives me an error, maybe because I'm using 5.8.0, but it's given me food for thought and I'll try to find a few hours to make some sense of the File::Find syntax. Got to make progress some time :-) JD
Re: -d
On Saturday, November 2, 2002, at 10:07 PM, John Delacour wrote: If I do this... #!/usr/bin/perl $dir = "$ENV{HOME}/"; opendir DIR, $dir ; for (readdir DIR) { -d and print "$dir$_$/" } I aim to get all the files in my home directory that are directories, but the result is a list of only a few of the existing directories: /Users/jd/. /Users/jd/.. /Users/jd/dev /Users/jd/Library That's because it's testing the names in $_ relative to your *current* directory, not relative to $ENV{HOME}. Try this instead: #!/usr/bin/perl $dir = "$ENV{HOME}/"; opendir DIR, $dir ; for (readdir DIR) { -d "$dir$_" and print "$dir$_$/" } How do I get a list of all directories in a directory and, more to the point, what is the best way to get a complete tree starting from a certain directory? File::Find, as others have mentioned. -Ken
Re: -d
If I do this... #!/usr/bin/perl $dir = "$ENV{HOME}/"; opendir DIR, $dir ; for (readdir DIR) { -d and print "$dir$_$/" } I aim to get all the files in my home directory that are directories, but the result is a list of only a few of the existing directories: It works for me. Are you sure you know what $ENV{HOME} resolves to? ? What version of OS X are you running? What version of perl? I'm running 10.2 with the stock perl. How do I get a list of all directories in a directory and, more to the point, what is the best way to get a complete tree starting from a certain directory? 'Best' depends on what you're trying to do. The File::Find method posted earlier is a good one, yours is a good one (that should work). You might prefer the File::Find method if you're planning on using your script on a variety of platforms and machines, as it will be less dependant on platform specific conditions. You might prefer your solution (if it worked for you) if you were in a high performance environment and needed every possible last cycle you could get, or in an environment where you want to keep your perl installation as small as possible and didn't want to add File::Find (although it's part of the core install, I b'lve) -Jeff Lowrey
Re: -d
here is a code example using find:file, note it is recursive, and will work with where you are sitting, or a passed argument of start point including ~ Dave Gomez #!/usr/bin/perl -lw # fdirs - find all directories @ARGV = qw(.) unless @ARGV; use File::Find (); sub find(&@) { &File::Find::find } *name = *File::Find::name; find { print $name if -d } @ARGV; On 11/2/02 4:07 AM, "John Delacour" <[EMAIL PROTECTED]> wrote: > If I do this... > > #!/usr/bin/perl > $dir = "$ENV{HOME}/"; > opendir DIR, $dir ; > for (readdir DIR) { > -d and print "$dir$_$/" > } > > I aim to get all the files in my home directory that are > directories, but the result is a list of only a few of the existing > directories: > > /Users/jd/. > /Users/jd/.. > /Users/jd/dev > /Users/jd/Library > > How do I get a list of all directories in a directory and, more to > the point, what is the best way to get a complete tree starting from > a certain directory? > > JD > >
-d
If I do this... #!/usr/bin/perl $dir = "$ENV{HOME}/"; opendir DIR, $dir ; for (readdir DIR) { -d and print "$dir$_$/" } I aim to get all the files in my home directory that are directories, but the result is a list of only a few of the existing directories: /Users/jd/. /Users/jd/.. /Users/jd/dev /Users/jd/Library How do I get a list of all directories in a directory and, more to the point, what is the best way to get a complete tree starting from a certain directory? JD