473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Opening a file and outputing the contents

Alright I can't seem to get this to actually read from the file but I believe I'm doing it correctly, but obviouslly I'm not.

Expand|Select|Wrap|Line Numbers
  1. my $fileName = "guessingGame.csv";
  2.  
  3. sub load()
  4. {    
  5.     open (MYFILE, $fileName) || die(print "Could not open file!");
  6.     while (<MYFILE>) 
  7.     {
  8.         chomp;
  9.         print "$_\n";
  10.     }
  11.     close (MYFILE); 
  12. }
Feb 23 '09 #1
4 1410
nithinpes
410 Expert 256MB
There is nothing wrong in the script. It should work once you call the subroutine:
Expand|Select|Wrap|Line Numbers
  1. &load();   
  2.  
Feb 23 '09 #2
If I'm using windows is their a difference?
Feb 23 '09 #3
KevinADC
4,059 Expert 2GB
@chanshaw
No there is no difference. Your open() function is inside a subroutine (well really a prototype in your case) so you have to call the subroutine to tell perl to run the subroutine.

Expand|Select|Wrap|Line Numbers
  1. my $fileName = "guessingGame.csv";
  2. load();#<-- now perl will run the subroutine
  3.  
  4.  
  5. sub load {    
  6.     open (MYFILE, $fileName) || die(print "Could not open file!");
  7.     while (<MYFILE>)  {
  8.         chomp;
  9.         print "$_\n";
  10.     }
  11.     close (MYFILE); 
  12. }
  13.  
Don't use parentheses after subroutine names:

sub load() {

That is a called a prototype and you certainbly have no reason to use them and probably never will so just drop the parentheses from now on:

sub load {

You should also pass arguments to your subroutines and pass data back although in this case there is no data to pass back really since you are only using the subroutine to open a file, print its contents and nothing else. Not really a good use of a subroutine.
Feb 23 '09 #4
numberwhun
3,509 Expert Mod 2GB
@chanshaw
There shouldn't be a difference for the code you have shown us. What happened when you ran the subroutine as Nithinpes suggested?

Also, the only thing that I see is in your die() statement. If you look at the documentation, you don't need the print inside of it as whatever is in the quotes gets printed by the die function already. You just put in the message you want to have output if the open fails.

Regards,

Jeff
Feb 23 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
14
by: Sin | last post by:
I've tried everything and I've come to the conclusion that I'm screwed. If anyone can help, I would be eternaly greatful. I have a solution which has 47 projects.. This is includes (very roughly)...
2
by: Arfur Million | last post by:
Hello, I hope it's OK to post beginner's questions to this group? If not, please direct me to the right one. I'm having trouble with the following program, which is taken from SAMS Teach...
8
by: Mantorok | last post by:
Hi all I have the contents of a text file that I would like to output to the response stream, however IE thinks it is XML and comes up with and error, here is the code: Response.ContentType =...
16
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
15
by: Sandra-24 | last post by:
I was reading over some python code recently, and I saw something like this: contents = open(file).read() And of course you can also do: open(file, "w").write(obj) Why do they no close...
1
by: JanaB | last post by:
I am writing a code that needs to read in a binary file. At the moment I don't have to do anything with the data, I just need to view the contents to compare its structure with another binary file....
1
by: raghu | last post by:
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')-------->for writiing i.write('hai')--------->written some content in text file i =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.