perl assignment question

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 11
Reputation: mtramnes is an unknown quantity at this point 
Solved Threads: 0
mtramnes mtramnes is offline Offline
Newbie Poster

perl assignment question

 
0
  #1
Feb 10th, 2009
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.

  1. #!/usr/bin/perl -w
  2. # Assignment3Ex1.pl
  3.  
  4. use strict;
  5.  
  6. open(INFH, '<', 'gettysburgh.txt') or die $!;
  7. open(OUTFH, '>', 'ex1out.txt') or die $!;
  8.  
  9. while (<INFH>) {
  10. next if /^\s*$/;
  11. my @words = split;
  12. print OUTFH "$_\n" foreach @words;
  13. }
  14.  
  15. close INFH;
  16. close OUTFH;
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
0
  #2
Feb 10th, 2009
try using the full path to the file:

open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: perl assignment question

 
0
  #3
Feb 10th, 2009
Originally Posted by KevinADC View Post
try using the full path to the file:

open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
I find this open statement interesting... I always do something like this:

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
0
  #4
Feb 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: perl assignment question

 
0
  #5
Feb 10th, 2009
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


Originally Posted by KevinADC View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: perl assignment question

 
0
  #6
Feb 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: perl assignment question

 
0
  #7
Feb 10th, 2009
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


Originally Posted by Comatose View Post
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
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
0
  #8
Feb 11th, 2009
Originally Posted by Comatose View Post
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
Thats DOS (or other shell), not Windows. DOS uses forward slashes as switches, so you can't use them in directory paths. But Windows does fully support them. Its Windows itself that translates forward slashes into backslashes. Try using backslashes in Unix directry paths with perl or python and see what happens.
Last edited by KevinADC; Feb 11th, 2009 at 3:03 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: perl assignment question

 
0
  #9
Feb 11th, 2009
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.
Last edited by KevinADC; Feb 11th, 2009 at 2:59 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: perl assignment question

 
0
  #10
Feb 11th, 2009
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.

Originally Posted by KevinADC View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC