in reply to Port check - Services File

The code you posted doesn't compile. Please post a compiling code snippet and also a sample of what you have in /etc/services. (See Write-up formatting tips.)

Here's a rewrite of your code with several comments so you can see what's different:

( Edit: as hippo points out below, it's not likely that your /etc/services file actually contains lines containing only port numbers. This example code does not address that issue, nor try to use regular expression matching; it's just an example of how to work through your two data sets. )

#!usr/bin/perl use strict; # don't leave home without it use warnings; # same here! use 5.10.0; # so you can use say() my @ports = qw( 21 22 23 ); # make a hash from the array for easier lookup my %ports = map { $_ => 1 } @ports; # use a lexical variable for your filehandle open my $SERVICES_FILE, '<', '/etc/services' or die $!; # read one line at a time with while() while ( my $line = <$SERVICES_FILE> ) { # deal with new-line character at end of line chomp( $line ); # 'postfix if' simplifies syntax say "Match Found: $line" if $ports{ $line }; } # good practice to close filehandle close $SERVICES_FILE or die $!; # good practice to tell Perl when the program's done __END__

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Port check - Services File
by deelinux (Novice) on Dec 02, 2015 at 15:58 UTC

    many thanks, notes taken, will play now