Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Net:Addr

by drock (Beadle)
on Dec 04, 2004 at 05:45 UTC ( #412362=perlquestion: print w/replies, xml ) Need Help??

drock has asked for the wisdom of the Perl Monks concerning the following question:

I have this code wherein I would like to determine if the number is a valid ip address. But I keep getting this error: 23.23.109.103 Undefined subroutine &main::ipv4check_ip called at number_test.pl line 15 (#1) (F) The subroutine indicated hasn't been defined, or if it was, it has since been undefined. Uncaught exception from user code: Undefined subroutine &main::ipv4check_ip called at number_test.pl line 15. Can anyone help please! thank you!
#!/usr/local/bin/perl use strict; use warnings; use diagnostics; use Net::IPv4Addr qw( :all ); my $var = "23.23.109.103"; print "$var\n"; #if ($var =~ m/^\d+$/) { #if ($var !~ /\D/) { if ( $var = ipv4check_ip($var)) { print "your variable is a number $var\n"; } else { print "number $var is not a number\n"; }

Replies are listed 'Best First'.
Re: Net:Addr
by Zed_Lopez (Chaplain) on Dec 04, 2004 at 05:49 UTC

    Try ipv4_checkip instead of ipv4check_ip.

      ok now I am getting:
      23.23.109.103 Use of inherited AUTOLOAD for non-method Net::IPv4Addr::ipv4_checkip() + is deprecated at number_test.pl line 15 (#1) (D deprecated) As an (ahem) accidental feature, AUTOLOAD subroutin +es are looked up as methods (using the @ISA hierarchy) even when the subroutines to be autoloaded were called as plain functions (e.g. Foo::bar()), not as methods (e.g. < Foo-bar() >> or < $obj-bar() >>). This bug will be rectified in future by using method lookup only f +or methods' AUTOLOADs. However, there is a significant base of exist +ing code that may be using the old behavior. So, as an interim step, +Perl currently issues an optional warning when non-methods use inherite +d AUTOLOADs. The simple rule is: Inheritance will not work when autoloading non-methods. The simple fix for old code is: In any module that +used to depend on inheriting AUTOLOAD for non-methods from a base class named BaseClass, execute *AUTOLOAD = \&BaseClass::AUTOLOAD during startup. In code that currently says use AutoLoader; @ISA = qw(AutoLoader); you should remove AutoLoader from @ISA and change use AutoLoader; +to use AutoLoader 'AUTOLOAD';. Can't locate auto/Net/IPv4Addr/ipv4_checki.al in @INC (@INC contains: /usr/local/lib /usr/local/lib/perl5/5.8.0/sun4-solaris /usr/lo +cal/lib/perl5/5.8.0 /usr/local/lib/perl5/5.8.0/sun4-solaris/CORE /usr/local/lib/perl5/site +_perl/5.8.0/MIME /us r/local/lib/perl5/5.8.0/CGI/ /usr/local/bin/perld/modules/Net-IPv4Addr +-0.09/blib /usr/loca l/lib/perl5/5.8.0/sun4-solaris /usr/local/lib/perl5/5.8.0 /usr/local/l +ib/perl5/site_perl/5 .8.0/sun4-solaris /usr/local/lib/perl5/site_perl/5.8.0 /usr/local/lib/ +perl5/site_perl .) a t number_test.pl line 15 (#2) (F) You said to do (or require, or use) a file that couldn't be found. Perl looks for the file in all the locations mentioned in @ +INC, unless the file name included the full path to the file. Perhaps +you need to set the PERL5LIB or PERL5OPT environment variable to say w +here the extra library is, or maybe the script needs to add the library + name to @INC. Or maybe you just misspelled the name of the file. See perlfunc/require and lib. Uncaught exception from user code: Can't locate auto/Net/IPv4Addr/ipv4_checki.al in @INC (@INC co +ntains: /usr/local/l ib /usr/local/lib/perl5/5.8.0/sun4-solaris /usr/local/lib/perl5/5.8.0 +/usr/local/lib/perl5 /5.8.0/sun4-solaris/CORE /usr/local/lib/perl5/site_perl/5.8.0/MIME /us +r/local/lib/perl5/5. 8.0/CGI/ /usr/local/bin/perld/modules/Net-IPv4Addr-0.09/blib /usr/loca +l/lib/perl5/5.8.0/su n4-solaris /usr/local/lib/perl5/5.8.0 /usr/local/lib/perl5/site_perl/5 +.8.0/sun4-solaris /u sr/local/lib/perl5/site_perl/5.8.0 /usr/local/lib/perl5/site_perl .) a +t number_test.pl lin e 15 AutoLoader::AUTOLOAD(23.23.109.103) called at number_test.pl l +ine 15
      My PERL5LIB path is
      /usr/local/lib:/usr/local/lib/perl5/5.8.0:/usr/local/lib/perl5/5.8.0/s +un4-solaris/CORE:/us r/local/lib/perl5/site_perl/5.8.0/MIME:/usr/local/lib/perl5/5.8.0/CGI/ +:/usr/local/bin/perl d/modules/Net-IPv4Addr-0.09/blib

      2004-12-04 Janitored by Arunbear - added code tags, as per Monastery guidelines

        Ah, the POD lies. It's ipv4_chkip. Your code works for me with this change.

Re: Net:Addr
by fokat (Deacon) on Dec 05, 2004 at 01:52 UTC

    Hi there:

    Of course I am biased, but how about... (untested)

    use strict; use warnings; use vars qw($is_this_an_IP_address); use NetAddr::IP; my $ip = new NetAddr::IP $is_this_an_IP_address; if ($ip) { # This is a valid IP address, or something an IP address can be deri +ved # from } else { # This is not an IP address }

    Best regards

    -lem, but some call me fokat

Re: Net:Addr
by Zed_Lopez (Chaplain) on Dec 07, 2004 at 02:38 UTC

    I went to report the doc error, and found a major, easy-to-fix bug that's been ignored for two years. ipv4_chkip will pass any string of four or more integers between 0 and 255 connected by .'s. The deal-breaker here is the 'four or more' instead of 'exactly four.'

    So I can't recommend it. Perhaps try Regexp::Common instead.

    use Net::IPv4Addr qw(:all); use Regexp::Common qw(net); my @var = qw(23.23.109.103 12324.123.12.1 1.2.3.4.5.6); print "Net::IFv4Addr:\n"; ipv4_chkip($_) and print "$_ is good\n" for @var; print "Regexp::Common:\n"; /^$RE{net}{IPv4}$/ and print "$_ is good\n" for @var;

    produces

    Net::IFv4Addr: 23.23.109.103 is good 1.2.3.4.5.6 is good Regexp::Common: 23.23.109.103 is good

    Note that you do need to anchor the start and end of the regexp with ^ and $, 'cause just /$RE{net}{IPv4}/ would match 312.45.32.4.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://412362]
Approved by dondelelcaro
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2020-11-26 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?