[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: ARRGH!




Ken McGill writes recently:

>Well I figured out how to create a Library, I just needed to save
>the lib in text mode with MacPERL and not with simple text.  Anyway,
>I am just trying to create and write to a file now and this what
>I tried
>
>open(handle, 'look.out');
>print handle 'stuff\n';
>close(handle);
>
>with no success. What am I missing?

A few small things (punctuation actually). To open a fresh new file for writing 
into:

 open(HANDLE, '>look.out');
 print HANDLE "stuff\n";
 close(HANDLE);

and to simply append the word 'stuff' to the end of a pre-existing file try 
this:

 open(HANDLE, '>>look.out');
 print HANDLE "stuff\n";
 close(HANDLE);

There are several things to note about this: the use of the > or >> 
character(s) in the call to open(); the use of an uppercase HANDLE to avoid any 
possible clash with a perl reserved word (usually this is not a problem in perl 
as $if @then %when are perfectly valid perl data types - but FILEHANDLEs are 
bit different. Do note that in the case of 'handle' there really is no problem 
as there is no perl reserved word of that name - but good programming suggests 
the upper case); note the use of the double quotation marks in the call to 
print(); print HANDLE 'stuff\n'; would have literally printed:
stuff\n
in the file.

By the way, the "new Camel" aka O'Reilly's "Programming Perl 5 Second Edition" 
is now available see http://www.ora.com/catalog/pperl2/noframes.html for 
ordering info - it should be available in Barnes & Noble bookstores this week.

Peter Prymmer
pvhp@forte.com