From: mmccaws2 on
Hi

I'm trying to understand the I/O open function better. I got the test
script to work, but I not sure if the syntax is standard. Could
someone explain the syntax option, or point to a link, for using open
to run OS commands.

use strict;
my $line;


open (NS_COMMAND, 'nslookup -type=soa wa.gov. ns1.six.net. |')
or die "Ooops, $!";
my $count=1;
while ($line = <NS_COMMAND>) {
chomp($line);
print "$count $line\n";
$count++;
}
close NS_COMMAND;

i plan to morph this test script into a routine that runs through of
list of nslookup options. Also, since this will be finally ran on
[u|li]nux environment then any suggestions specific to the OS would be
greatful

Oh I need a perl/regex script that filters level 8 interference out of
getting work done. Best suggestion gets a embroidery patch with that
script on it.

Mike

From: Sherm Pendley on
"mmccaws2" <mmccaws(a)comcast.net> writes:

> I'm trying to understand the I/O open function better.

Have you read "perldoc perlopentut"?

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
From: Paul Lalli on
mmccaws2 wrote:
> I'm trying to understand the I/O open function better. I got the test
> script to work, but I not sure if the syntax is standard. Could
> someone explain the syntax option, or point to a link, for using open
> to run OS commands.

perldoc -f open
perldoc perlopentut
perldoc perlipc

>
> use strict;

Good!

> my $line;

.... not so good. Declare your variables in the shortest scope
possible. It will help immensely in debugging your scripts.

> open (NS_COMMAND, 'nslookup -type=soa wa.gov. ns1.six.net. |')
> or die "Ooops, $!";

This code is perfectly syntactically valid, but it's not the best. At
the very least, you should be using lexical filehandles, rather than
global barewords. That way, they're subject to strictures, two
independent pieces of code that use the same filehandle name can't
interphere with each other, and they're automatically closed when the
scope ends.

open my $NS_COMMAND, 'nslookup -type=soa wa.gov. ns1.six.net. |') or
die "...";

These days, it's also preferred to use the three-argument form of open,
rather than specifying the mode within the filename (or program name,
in the case of open() for IPC)

open my $NS_COMMAND, '-|', 'nslookup', '-type=soa wa.gov.',
'ns1.six.net.' or die "...";

(note that this doesn't really have any effect on your specific case,
but it's best to use the best practice always, to get into the habbit.
It can affect things greatly when you eventually go to open a file
named by a variable whose value was read from the user...)

> my $count=1;

This variable is pointless, as Perl already defines $. for you...

> while ($line = <NS_COMMAND>) {

Here, btw, is where $line should have been defined.

> chomp($line);
> print "$count $line\n";

Why are you chomping only to print a newline at the end anyway?

> $count++;
> }

Taking into account these changes, your loop becomes:

while (my $line = <$NS_COMMAND>) {
print "$. $line";
}

> close NS_COMMAND;
>
> i plan to morph this test script into a routine that runs through of
> list of nslookup options. Also, since this will be finally ran on
> [u|li]nux environment then any suggestions specific to the OS would be
> greatful

No, that's not what you want. You should be asking for suggestions
that are platform-agnostic, not simply programs that are not tailored
to other operating systems. The above code is indeed platform
independent.

Paul Lalli

From: Mark Donovan on
On 1/17/07 12:13, "mmccaws2" <mmccaws(a)comcast.net> wrote:

> [snip]
>
> i plan to morph this test script into a routine that runs through of
> list of nslookup options. Also, since this will be finally ran on
> [u|li]nux environment then any suggestions specific to the OS would be
> greatful
>

Instead of the nslookup command, the Net::DNS module from CPAN might work
for you. See http://www.net-dns.org/

Here's Net::DNS doing the same thing as your nslookup command.

===
use Net::DNS;
use Data::Dumper; # not required, used to show what query returns

my $res = Net::DNS::Resolver->new(
nameservers => [qw(ns1.six.net)],
);
my $query = $res->query('wa.gov', 'SOA') or
die "query failed: $res->errorstring";
($query->answer)[0]->print;
print Dumper($query->answer);
===

This prints:

wa.gov. 2711 IN SOA disdns1.wa.gov. bruceh.dis.wa.gov. (
200701073 ; Serial
10800 ; Refresh
3600 ; Retry
604800 ; Expire
86400 ) ; Minimum TTL
$VAR1 = bless( {
'minimum' => 86400,
'serial' => 200701073,
'ttl' => 2711,
'mname' => 'disdns1.wa.gov',
'name' => 'wa.gov',
'rdata' => '<raw packet data>',
'retry' => 3600,
'refresh' => 10800,
'rdlength' => 43,
'type' => 'SOA',
'class' => 'IN',
'expire' => 604800,
'rname' => 'bruceh.dis.wa.gov'
}, 'Net::DNS::RR::SOA' );

--


From: John W. Krahn on
Paul Lalli wrote:
> mmccaws2 wrote:
>>
>> open (NS_COMMAND, 'nslookup -type=soa wa.gov. ns1.six.net. |')
>> or die "Ooops, $!";
>
> This code is perfectly syntactically valid, but it's not the best. At
> the very least, you should be using lexical filehandles, rather than
> global barewords. That way, they're subject to strictures, two
> independent pieces of code that use the same filehandle name can't
> interphere with each other, and they're automatically closed when the
> scope ends.
>
> open my $NS_COMMAND, 'nslookup -type=soa wa.gov. ns1.six.net. |') or
> die "...";
>
> These days, it's also preferred to use the three-argument form of open,
> rather than specifying the mode within the filename (or program name,
> in the case of open() for IPC)
>
> open my $NS_COMMAND, '-|', 'nslookup', '-type=soa wa.gov.',
^
That's not going to work with that space character in there.

> 'ns1.six.net.' or die "...";

Either use a single string:

open my $NS_COMMAND, '-|', 'nslookup -type=soa wa.gov. ns1.six.net.' or die "...";

Or a list with no whitespace:

open my $NS_COMMAND, '-|', 'nslookup', '-type=soa', 'wa.gov.', 'ns1.six.net.'
or die "...";




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 |  Next  |  Last
Pages: 1 2 3 4
Prev: parse body mime message
Next: regex problem