JR wrote:
> Greetings everyone,
> 
> I'm very new to perl, so please don't laugh at my script ;-)
> I wrote this script to login to my work box from home and change a
> hosts 
> file to reflect my home ip address. I know there is a way to do this a
> lot quicker and prettier. This is really just a learning experience
> for 
> me, so any input would be appreciated. I just started reading
> "Mastering 
> Regular Expressions", so that's still pretty new to me. Anyway, here
> we go: 

<snip>

> my $ppp0 = `/sbin/ifconfig ppp0`;
> $ppp0 =~ m/(\d+\.\d+\.\d+\.\d+)/;
> my $ip = $1;

I'm certainly far from a Perl expert, but one suggestion I do have is to
be careful to test that your regular expression does have a match before
using $1. I believe $1 contains the first match from the last
_successful_ regex, not just the last regex. I would change the above
snippit to something along the lines of:

my $ppp0 = `/sbin/ifconfig ppp0`;
die "Invalid output from ifconfig! Received: $ppp0\n"
  unless ($ppp0 =~ m/(\d+\.\d+\.\d+\.\d+)/);
my $ip = $1;

Otherwise, if you had a successfully matching regex before this one and,
for some unknown reason, this regex failed, you might be populating $ip
with bogus info.

Hope that helps!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to