CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Pre-building pattern match string triggers "Use of uninitialized value $_" warning

 



bigteks
Novice

Feb 10, 2016, 1:03 PM

Post #1 of 9 (3269 views)
Pre-building pattern match string triggers "Use of uninitialized value $_" warning Can't Post

Several of the examples in this library I am looking at, assemble the pattern matching string in advance and use it later, and they all seem to generate a warning but work fine otherwise. Here is an example:

Code
#!/bin/perl  

use strict;
use warnings;

use Socket;

my $ADDR_PAT = /^\d+\.\d+\.\d+\.\d+$/;

while (<>) {
chomp;
die "$_: Not a valid address" unless /$ADDR_PAT/o;
my $name = gethostbyaddr(inet_aton($_),AF_INET);
$name ||= '?';
print "$_ => $name\n";
}


Warning generated:

Use of uninitialized value $_ in pattern match (m//) at ./validate-addr-by-regex.pl line 8.


Can I still structure this the same way (prebuilding the pattern match string) but eliminate the warning?


(This post was edited by FishMonger on Feb 10, 2016, 1:06 PM)


FishMonger
Veteran / Moderator

Feb 10, 2016, 1:07 PM

Post #2 of 9 (3265 views)
Re: [bigteks] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

You forgot to put quotes around the pattern.

Code
my $ADDR_PAT = '/^\d+\.\d+\.\d+\.\d+$/';



bigteks
Novice

Feb 10, 2016, 1:13 PM

Post #3 of 9 (3263 views)
Re: [FishMonger] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

I thought the same thing. This is copied straight from their code library as-is. At least in my environment, when I add the quotes, which are not in the original code, the warning goes away but it doesn't work, the pattern match doesn't match IPs any more. Without the quotes, it generates a warning but successfully matches the IPs that I type in.


FishMonger
Veteran / Moderator

Feb 10, 2016, 1:14 PM

Post #4 of 9 (3259 views)
Re: [bigteks] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post


Code
die "$_: Not a valid address" unless $ADDR_PAT;



bigteks
Novice

Feb 10, 2016, 1:16 PM

Post #5 of 9 (3255 views)
Re: [FishMonger] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

Perfect (slaps head with hand)


(This post was edited by bigteks on Feb 10, 2016, 1:17 PM)


FishMonger
Veteran / Moderator

Feb 10, 2016, 1:19 PM

Post #6 of 9 (3250 views)
Re: [bigteks] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

I'd probably write the pattern using the qr() operator.

Code
my $ADDR_PAT = qr(/^\d+\.\d+\.\d+\.\d+$/);



FishMonger
Veteran / Moderator

Feb 10, 2016, 1:22 PM

Post #7 of 9 (3248 views)
Re: [bigteks] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

Are you getting these examples from the official perl documentation, or from a book?


(This post was edited by FishMonger on Feb 10, 2016, 1:23 PM)


bigteks
Novice

Feb 10, 2016, 1:32 PM

Post #8 of 9 (3243 views)
Re: [FishMonger] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

Web site:

http://www.java2s.com/Code/Perl/CatalogPerl.htm


FishMonger
Veteran / Moderator

Feb 10, 2016, 2:23 PM

Post #9 of 9 (3235 views)
Re: [bigteks] Pre-building pattern match string triggers "Use of uninitialized value $_" warning [In reply to] Can't Post

That's a very poor site to use for learning perl.

A lot of their examples will generate warnings assuming they have warnings enabled, which they often don't. A number of their examples are bad enough that they won't compile under strict.

Every perl script you write should include the warnings and strict pragmas which a lot of their examples don't.

Use the official documentation, which you can access on your system via the perldoc command line utility.

Quote
perldoc perl


Or via online. http://perldoc.perl.org/

Another good resource is http://perl-begin.org/
or https://pragprog.com/book/swperl/modern-perl-fourth-edition


(This post was edited by FishMonger on Feb 10, 2016, 2:26 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives