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

Re: [MacPerl] Code cleaning and questions

On Thu, Jun 24, 1999 at 09:48:04AM -0500, Robert Pollard wrote:
> Hello everyone,
> I have a snippet of code I would like to ask questions about.  I am a
> newbie so I won't even raise your brainwave level far above a coma.
> I did this little bit of code to check the contents of a file for any
> strange characters.  The file was causing a freeze and I thought there
> may be something odd in it.  It turned out there wasn't but I have some
> questions about it.
> open(SOURCE, '<Robert:Desktop Folder:Addresses') or die;
> open(DEST, '>Robert:Desktop Folder:Addresses.clean') or die;
> 1)   Does the open command always read the whole file in.

The open() function does not read any of the file in.  It just creates a
filehandle that you can use to read the file in later.  (Or write to, or
whatever, as appropriate.)

> This is an
> interesting syntax because what this did was to put the whole contents
> of a file line by line into an array.  What if my text were very long
> paragraphs and exceeded the maximum character limit of the variable for
> each element?
>    @line = <SOURCE>;

I'm not sure what would happen if you had a line that was longer than the
maximum character limit.  I believe that limit is somewhere between 255
megabytes and 4095 megabytes for a single scalar value, at least on the
system I did a bit of testing on just now - at the upper end, the scalar
came back empty.  Anyway, it's more likely MacPerl will run out of memory
before you manage to fill up a scalar variable.

> 2)  Why do I need to chomp.  The program ran fine and put everything
> aout like it was suppose to.  I believe this is for stripping the CRs at
> the end of the line?
>    #chomp($line);

Yes, that is what it is for.  If you're not _sure_ what chomp() does, why
didn't you read the documentation???

If you don't want to remove the newlines, you don't need to chomp().

> print "@line\n";

Are you sure this is what you want to do?  This prints with a space between
each array element, and adds an extra newline at the end.  You probably
print @line;

Read the documentation on quote operators and the $" variable.

> foreach $line (@line) {
>    ($Name, $Phone, $Address, $Type) = split(/\t/, $line);  # This cool.
> I would use this before I would allow the split to assign each line
> # to different elements in the array.  In the importation of records,
> this seems to be the best way to write to arrays in row/column fashion.
> 3)   Here I didn't have to use the angle brackets around the
> filehandle.  If I did it seemed to refer to the whole file.  Without
> them, it allows whatever you want to put out.  As in this case I was
> able to put out a whole line at a time.
>    print DEST $line;

Angle brackets around a filehandle are the _input_ operator.  If you're
trying to output to a filehandle, you should not use the input operator on
it.  Once again, why haven't you read the documentation???

>    print("The name is $Name.\n");
> # This is where I convert each character to the ascii representation.
>    print("The ascii representation is:\n");
>    for ($i=1;$i <= length($line);$i++) {
>       $num = ord(substr($line, $i, 1));
>       print("$num,");
>       if ($num > 127) {
>         print ("\n\nW A R N I N I N G -- ASCII value exceeds 127\n\n");
>       }
>    }
>    print("\n");
> }
> close(SOURCE);
> close(DEST);
> 4)   Could you guys give me any tips/hints on code optimization?

Tip #1.  Read the documentation.

Tip #2.  Try to program in Perl, not in C.  The for loop above is how you
would solve this problem in C.  A more Perlish way would be:

foreach (split //, $line) {
  print $num = ord $_;
  $num > 127 and warn "WARNING -- ASCII value exceeds 127.\n";


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org