in reply to Change directories, Print just the comments

No need to change to /etc directory if you only want to read the services file. Just use the full path to the services file.
use strict; use warnings; use IO::File; my $f = new IO::File "/etc/services", "r" or die "can not read services file"; while (<$f>) { print "$1\n" if /(#.*)$/; }
And you don't really need perl at all to do this on *nix -
grep \# /etc/services | cut -f2 -d#

Replies are listed 'Best First'.
Re: (2) Change directories, Print just the comments
by The Mad Hatter (Priest) on Dec 09, 2003 at 00:38 UTC
    While technically correct, I'd argue that there is no reason to bring IO::File into this (especially for the OP, who obviously doesn't need to mess with that module now). Perl's open will work just fine; why use a module when you don't need to?
      Well, IO::File is my preferred way for file I/O, I always use it unintentionally. I agree the OP is probably a bit green on Perl, but it shouldn't hurt to show him another way of openning files, should it. ;-)
        You're right that most of the time it won't hurt. That said, I don't think it will help most of the time either, especially if the OP gets into the habit of using it, even though she doesn't know why she should use it (if she should use it at all). In my opinion, cargo cult programming in this case would lead to an incomplete understanding of basic functions and when it is appropriate to use modules. I'm curious though, why is it your preferred method of file I/O?