943,883 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1819
  • Perl RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 10th, 2009
0

perl assignment question

Expand Post »
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)
  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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mtramnes is offline Offline
11 posts
since Dec 2008
Feb 10th, 2009
0

Re: perl assignment question

try using the full path to the file:

open(INFH, '<', 'full/path/to/gettysburgh.txt') or die $!;
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Feb 10th, 2009
0

Re: perl assignment question

Click to Expand / Collapse  Quote originally posted by KevinADC ...
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.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Feb 10th, 2009
0

Re: perl assignment question

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
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Feb 10th, 2009
0

Re: perl assignment question

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


Click to Expand / Collapse  Quote originally posted by KevinADC ...
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
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Feb 10th, 2009
0

Re: perl assignment question

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
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 10th, 2009
0

Re: perl assignment question

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


Click to Expand / Collapse  Quote originally posted by Comatose ...
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
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Feb 11th, 2009
0

Re: perl assignment question

Click to Expand / Collapse  Quote originally posted by Comatose ...
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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Feb 11th, 2009
0

Re: perl assignment question

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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Feb 11th, 2009
0

Re: perl assignment question

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.

Click to Expand / Collapse  Quote originally posted by KevinADC ...
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.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Print in Perl
Next Thread in Perl Forum Timeline: perl loop replacement for normalization





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC