| | |
perl assignment question
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 11
Reputation:
Solved Threads: 0
I have a perl assignment I have a question about. I have to take a text file (gettysburgh.txt) and I have to alter it. I think my code is correct but when I run the program it says no such file exists. The file is saved on my computer so I was hoping someone could tell me what I was doing wrong.
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl -w # Assignment3Ex1.pl use strict; open(INFH, '<', 'gettysburgh.txt') or die $!; open(OUTFH, '>', 'ex1out.txt') or die $!; while (<INFH>) { next if /^\s*$/; my @words = split; print OUTFH "$_\n" foreach @words; } close INFH; close OUTFH;
•
•
•
•
try using the full path to the file:
open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
open(INFH,"<\\full\\path\\to\\gettysburgh.txt") #on windows open(INFH,"</full/path/to/gettysburgh.txt") #on unix
What I don't understand is the ,'<', construct... that's not something I usually do.
And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is.
Larry Wall
Larry Wall
Window fully supports forward slashes in directory paths. It always has. You can start using them from now on. 
This is the three argument style of open:
open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
Its safer to use than the two argument style:
I'm pretty sure its convered in the open() functions documentation:
http://perldoc.perl.org/functions/open.html

This is the three argument style of open:
open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
Its safer to use than the two argument style:
I'm pretty sure its convered in the open() functions documentation:
http://perldoc.perl.org/functions/open.html
Yeah I use the forward slash "/" in windows as well and windows/perl supports it. One thing I HAVE found is that Apache for windows is the only app I have come across that supports directory (or folder-level) permissions. What I found was that if folders are set as read-only, the only way to take that attribute off and have apache (or mod-perl or cgi perl) control a particular directory is to go to the cmd line and use attrib to remove the read only traits at the folder level. That is the only windows-weirdness I have found (and I found it with Apache 2 only).
As for the 3 argument open statement, I have never seen it before, but, perhaps, if that is the way they teach it, I should start using it - haha.
Mike
As for the 3 argument open statement, I have never seen it before, but, perhaps, if that is the way they teach it, I should start using it - haha.
Mike
•
•
•
•
Window fully supports forward slashes in directory paths. It always has. You can start using them from now on.
This is the three argument style of open:
open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
Its safer to use than the two argument style:
I'm pretty sure its convered in the open() functions documentation:
http://perldoc.perl.org/functions/open.html
And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is.
Larry Wall
Larry Wall
I'm pretty sure windows does not support paths with / as the separator. Try this:
cd / or cd /windowsand then try this cd \windows. What I'm guessing happens here, is Perl (and other languages that do this, such as python) are smart enough to know that even though we hacker-esque folks use / in a path, that we really mean \, and naturally adjusts it Comatose,
You are correct at the shell level. - windows does NOT do that. I use the double backslashes in a file path because I usually set a data or file path (like $DATAROOT="\\a\\b\\c") and then substitute stuff in double quotes, rather than single - in which the double back quotes on windows work far better (i.e. "$DATAROOT\\$morepath\\$filespec"). Perl, python, ruby, java, etc. are smart enough to translate. Also, cygwin, which I always install on my windows systems (and which can use bash - which I love - btw I am I unix guy suffering through windows) can also do that translation. But ultimately, you are correct, the regular windows shell does NOT do that translation, only the apps - i.e. perl, python, ruby, cygwin do that.
Mike
You are correct at the shell level. - windows does NOT do that. I use the double backslashes in a file path because I usually set a data or file path (like $DATAROOT="\\a\\b\\c") and then substitute stuff in double quotes, rather than single - in which the double back quotes on windows work far better (i.e. "$DATAROOT\\$morepath\\$filespec"). Perl, python, ruby, java, etc. are smart enough to translate. Also, cygwin, which I always install on my windows systems (and which can use bash - which I love - btw I am I unix guy suffering through windows) can also do that translation. But ultimately, you are correct, the regular windows shell does NOT do that translation, only the apps - i.e. perl, python, ruby, cygwin do that.
Mike
•
•
•
•
I'm pretty sure windows does not support paths with / as the separator. Try this:cd /orcd /windowsand then try thiscd \windows. What I'm guessing happens here, is Perl (and other languages that do this, such as python) are smart enough to know that even though we hacker-esque folks use / in a path, that we really mean \, and naturally adjusts it
And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is.
Larry Wall
Larry Wall
•
•
•
•
I'm pretty sure windows does not support paths with / as the separator. Try this:cd /orcd /windowsand then try thiscd \windows. What I'm guessing happens here, is Perl (and other languages that do this, such as python) are smart enough to know that even though we hacker-esque folks use / in a path, that we really mean \, and naturally adjusts it
Last edited by KevinADC; Feb 11th, 2009 at 3:03 am.
You can see here listed under directory paths for Windows:
http://en.wikipedia.org/wiki/Path_(computing)
In fact they list DOS as also accepting both \ / in directory paths. Maybe there is a way in DOS to change the path seperator. But when I try it it does not work in DOS, but it does in Windows.
http://en.wikipedia.org/wiki/Path_(computing)
In fact they list DOS as also accepting both \ / in directory paths. Maybe there is a way in DOS to change the path seperator. But when I try it it does not work in DOS, but it does in Windows.
Last edited by KevinADC; Feb 11th, 2009 at 2:59 am.
Interesting. Like I said, I have cygwin installed and it gives me the unix commands (ls, grep, pwd, etc.) and it gives me bash in dos/windows. I always thought that that was why I got / to work. I put cyg-win before windows commands in my system so when I "cd" i am using the unix-ish version, not dos or windows.
•
•
•
•
You can see here listed under directory paths for Windows:
http://en.wikipedia.org/wiki/Path_(computing)
In fact they list DOS as also accepting both \ / in directory paths. Maybe there is a way in DOS to change the path seperator. But when I try it it does not work in DOS, but it does in Windows.
And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is.
Larry Wall
Larry Wall
![]() |
Similar Threads
- PERL Text Statistic Help (Perl)
- perl assignment question (Perl)
- The Pascal Triangle (Perl)
- Help with a Profile Managment System (Perl)
- HELP! What does this mean.......? (Visual Basic 4 / 5 / 6)
Other Threads in the Perl Forum
- Previous Thread: Print in Perl
- Next Thread: perl loop replacement for normalization
| Thread Tools | Search this Thread |






