Hi Sander. First of all, I guess this will feel like I'm ripping
your code apart. Try to imagine me as your best-friend
cardboard-cutout programmer with nothing but your interests
at heart. :)

Few people know what to make of Perl the first time they see
it. People try to force it into either C or shell, but it's
neither. Take a look at what I've written below, and keep an
open mind!

Sander wrote:
>
> This is my first time on this mailing list, so i'll start with a Hello
> (world) :)

World> ** HELLO SANDER **

> I have a prob with my first script (i have read Learning Perl, and (not
> yet readed) Programming Perl.
> Now i want to make a simple tool to read my logs (i've started simple
> with one log, i need to filter it later for only dropped packets from
> the firewall, or errormessages in other logs etc.)
> This is what i have so far:

> #!/usr/bin/perl -w

  use strict;   # Always
  use warnings; # Usually (same as -w qualifier but portable)

> open (ER,"/home/unicorn/Plscripts/error_log"); #opening error_log

Check the status of /all/ opens. 99% of the time your code should
look like

  open ER, '/home/unicorn/Plscripts/error_log' or die $!;

> open (EL, "home/unicorn/Plscripts/ERROR.LOG"); #opening ERROR.LOG

The same:

  open EL, '> /home/unicorn/Plscripts/ERROR.LOG' or die $!;

Your problem is almost certainly here. You have opened a second
file read-only with a relative path, and without checking the
status of 'open'. It should be opened write-destructive with
an absolute path.

> select (ERROR.LOG); #select ERROR.LOG to write

No need. The last opened output stream will be automatically
selected for you.

>  while (<ER>) { #as long as ER is open, read....

Comments don't make the code work, especially innaccurate
ones! If it were correct, your comment should be something
like

    # Read a record into $_ and, while that read has a 'defined' value ...

>       chomp;

This is often useful, but you later 'print' the same record
without adding back the newline character.

>      print "$_"; #print output to ERROR.LOG

  print "$_\n"; # or don't 'chomp' the record

>  } select (STDOUT); #make sure STDOUT is normal again

What for, if you're not going to use STDOUT again?

Also, putting it in the same line as the closing brace makes
it look like a qualifier for the block instead of the separate
statement that it is.

My code would look like

  use strict;
  use warnings;

  open ER, '/home/unicorn/Plscripts/error_log' or die $!;
  open EL, '> /home/unicorn/Plscripts/ERROR.LOG' or die $!;

  print while <ER>;

You write a darned good acorn! Oaks are next week. :)

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to