using strict

J

Johhny

Hello,

I am in the process of learning perl, One thing I am trying to work out
is if its possible to do a statement like this.

==SNIP==

my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);

==SNIP==

What im trying to do is set the variable $hOst IF the system is up. If
its not up it will exit.

Regards,

Johhny
 
J

Johhny

Hello,

This is what im trying to do. I have got a script that I want to test
the input of, Ive done that now that the input is valid I want to
ensure that the host Im trying to connect to is up before I progress
further down the script. If the host is up I would like to set a
variable "$hOst".

Here is some code

==SNIP==

if ( $ARGV[0] =~ m/(([0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})/ ) {
my $pIng = Net::ping->new();
my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);
#print "$ARGV[0] \n";
$pIng->close();
}
else {
print "$ARGV[0] is an invalid IP Address\n";
exit;
}

==SNIP==

Now I understand that the regex is very board but im just trying to
learn perl, If I change the line where the

my $hOst = ...

to

print "$ARGV[0] is alive.\n" if $pIng->ping($ARGV[0], 30);

Then it works, So I was going to try and cut down the steps and exit if
the host is not up.

Regards,

Johhny
 
B

Bart Van der Donck

Johhny said:
Hello,

This is what im trying to do. I have got a script that I want to test
the input of, Ive done that now that the input is valid I want to
ensure that the host Im trying to connect to is up before I progress
further down the script. If the host is up I would like to set a
variable "$hOst".

Here is some code

==SNIP==

if ( $ARGV[0] =~ m/(([0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})/ ) {
my $pIng = Net::ping->new();
my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);
#print "$ARGV[0] \n";
$pIng->close();
}
else {
print "$ARGV[0] is an invalid IP Address\n";
exit;
}

==SNIP==

Now I understand that the regex is very board but im just trying to
learn perl, If I change the line where the

my $hOst = ...

to

print "$ARGV[0] is alive.\n" if $pIng->ping($ARGV[0], 30);

Then it works, So I was going to try and cut down the steps and exit if
the host is not up.

This should do the trick:

#!/usr/bin/perl
use strict;
use warnings;
use Net::ping;
my $hOst;
my $ip = '199.181.132.250';
my $p = Net::ping->new();
if ($p->ping($ip)) { $hOst=$ip } else { exit }
$p->close();
print "System is up. \$hOst is now set to $hOst.\n";
 
B

Brian McCauley

Johhny said:
Subject: using strict

Your post has nothing to do with using strict.

All strict does is disable three features of the language. Theses
features are:

* Use of a undeclared variable is interpreted as a package variable
in the current package.

* Using an unrecognised word is interpreted as a literal string.

* Using a non-reference value in a reference context is interpreted as
a requst to do a lookup in the package symbol table hash.

All three of these have their uses but can be very confusing if Perl
thinks you are trying to use when when actually you've just made a
mistake. For this reason it is strongly recommened that they are
switched off when not needed.
I am in the process of learning perl, One thing I am trying to work out
is if its possible to do a statement like this.
my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);

It is posible. But it should not be. It should throw an error.

The my() has both a compile-time and a run-time effect and the if
qualifier only has any meaning at run-time.

What happens when you do...

my $hOst if 0;

....is that the variable $hOst gets declared (at compile time) but it
doesn't get initialized at run-time so it retains it's value from one
iteration to the next.

Some people exploit this to create static variables. This is a very bad
habit and one day the behaviour may change.
What im trying to do is set the variable $hOst IF the system is up. If
its not up it will exit.

my $hOst;
$hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);

Or

my $hOst = $pIng->ping($ARGV[0], 30) ? $ARGV[0] : undef;
 
B

Brian McCauley

Johhny said:
This is what im trying to do. I have got a script that I want to test
the input of, Ive done that now that the input is valid I want to
ensure that the host Im trying to connect to is up before I progress
further down the script. If the host is up I would like to set a
variable "$hOst".

If you are not going to progress further down the script why does it
matter if $hOst is set or not?
if ( $ARGV[0] =~ m/(([0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})/ ) {
Now I understand that the regex is very board but im just trying to
learn perl,

Pre-authored regular expressions for things like IPv4 addresses can be
found in Regex::Common.
 
T

Tad McClellan

Johhny said:
Hello,

I am in the process of learning perl, One thing I am trying to work out
is if its possible to do a statement like this.

==SNIP==

my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);

==SNIP==


You should not do that. You should do this instead:

my $hOst;
$hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);


See:

perldoc perlsub

B<NOTE:> The behaviour of a C<my> statement modified with a statement
modifier conditional or loop construct (e.g. C<my $x if ...>) is
B<undefined>. The value of the C<my> variable may be C<undef>, any
previously assigned value, or possibly anything else. Don't rely on
it. Future versions of perl might do something different from the
version of perl you try it out on. Here be dragons.
 
J

Joe Smith

Johhny said:
Hello,

I am in the process of learning perl, One thing I am trying to work out
is if its possible to do a statement like this.

==SNIP==

my $hOst = $ARGV[0] if $pIng->ping($ARGV[0], 30);

==SNIP==

What im trying to do is set the variable $hOst IF the system is up.

my $host = ($ARGV[0] and $ping->ping($ARGV[0],30)) ? $ARGV[0] : undef;

This makes sure that the variable $host is declared, even if its
value is undefined.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,379
Messages
2,571,945
Members
48,805
Latest member
CeceliaWri

Latest Threads

Top