Perl Error Use Of Uninitialized Value In Pattern Match
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Use of uninitialized value in pattern match (m//) up vote 10 down vote favorite 1 I'm getting this error even though I have declared my regex variable. my $pattern = '(Cat\.\sNo\.\s\d+)'; Later in my code I use then $pattern. if ($page =~ /$pattern/) { push(@array, $element); } But when I run my code it gives me the below error, and continues to run successfully: Use of uninitialized value in pattern match (m//) A search on Google for this error seems to point to scenarios where the variable has not been initialised, although in my case it would seem I have initialised it already? perl share|improve this question edited Feb 29 '12 at 4:03 asked Feb 29 '12 at 3:43 kurotsuki 86541625 Could you show us more code? Where are $page and $element defined? I suspect a variable scope problem. –Toto Feb 29 '12 at 9:04 add a comment| 2 Answers 2 active oldest votes up vote 15 down vote accepted That message is not telling you that $pattern is uninitialized; it's telling you that $page is uninitialized. If you're expecting that $page might be uninitialized, and that's O.K., then you can bypass the warning, and make things clear for future readers of the source-code, by writing this: if (defined($page) && ($page =~ /$pattern/)) share|improve this answer edited Aug 9 at 16:39 answered Feb 29 '12 at 3:48 ruakh 94.6k10135187 Ok I've updated the code in the original question.. So in this case both $page and $element have been previously initialised with my $page and my $element. Yet I still get the uninitialised error? –kurotsuki Feb 29 '12 at 4:05 Just another note, I initialised $page and $element with default blank values, i.e. "" –kurotsuki Feb 29 '12 at 4:23 1 my $page doesn't initialize $page, it merely declares it. my $page; is e
How to get Help for Perl? Perl on the command line Core Perl documentation and CPAN module documentation POD - Plain Old Documentation Debugging Perl scripts Scalars Common Warnings and Error messages in Perl Automatic string to number conversion or casting in Perl Conditional statements, using if, else, elsif in Perl Boolean values in Perl Numerical operators String operators: concatenation (.), repetition (x) undef, the initial value and the defined function of Perl Strings in Perl: quoted, interpolated and escaped Here documents, or how to create multi-line strings in Perl Scalar http://stackoverflow.com/questions/9493304/use-of-uninitialized-value-in-pattern-match-m variables Comparing scalars in Perl String functions: length, lc, uc, index, substr Number Guessing game while loop Scope of variables in Perl Short-circuit in boolean expressions Files How to exit from a Perl script? Standard output, standard error and command line redirection Warning when something goes wrong What does die do? Writing to files with Perl Appending to files Open and read from http://perlmaven.com/use-of-uninitialized-value text files Don't Open Files in the old way slurp mode - reading a file in one step Lists and Arrays Perl for loop explained with examples Perl Arrays Processing command line arguments - @ARGV in Perl How to process command line arguments in Perl using Getopt::Long Advanced usage of Getopt::Long for accepting command line arguments Perl split - to cut up a string into pieces How to read a CSV file using Perl? join The year of 19100 Scalar and List context in Perl, the size of an array Reading from a file in scalar and list context STDIN in scalar and list context Sorting arrays in Perl Sorting mixed strings Unique values in an array in Perl Manipulating Perl arrays: shift, unshift, push, pop Reverse Polish Calculator in Perl using a stack Reverse an array, a string or a number The ternary operator in Perl qw - quote word Subroutines Subroutines and functions in Perl Variable number of parameters in Perl subroutines Understanding recursive subroutines - traversing a directory tree Hashes, arrays Hashes in Perl Creating a hash from an array in Perl Perl hash in sc
Test::Deep is a great module if you need to test deep and/or fuzzy data structures, but sometimes it does not give the correct error messages. Recently I've encountered such a case when I got tons of warnings like this: Use of uninitialized value $got in pattern match http://perlmaven.com/undef-in-test-deep-regex (m//) at Test/Deep/Regexp.pm line 57. Unfortunately the warning did not say what really caused it, http://www.linuxquestions.org/questions/programming-9/use-of-uninitialized-value-in-pattern-match-perl-888455/ but after patching the Test::Deep::Regexp I could solve the riddle. The problem was that in my test I had something like this: cmp_deeply \@data, array_each({ fname => re('^.*$'), lname => re('^.*$'), ... email => re('^.*$'), address => re('^.*$') }); with about 25 fields, each one with its own regex. Getting a warning that one of them is undef was not really useful. The perl error source code of Test/Deep/Regexp.pm looked like this: sub descend { my $self = shift; my $got = shift; my $re = $self->{val}; if (my $match_exp = $self->{matches}) { my $flags = $self->{flags}; my @match_got; if ($flags eq "g") { @match_got = $got =~ /$re/g; } else { @match_got = $got =~ /$re/; } if (@match_got) { return Test::Deep::descend(\@match_got, $match_exp); } else { return 0; } } else { return ($got =~ $re) ? 1 : 0; } } Line perl error use 57 in the original code, that generated the warning was this line: return ($got =~ $re) ? 1 : 0;. A quick glance at the return value gave me the impression that this would indicate the success or failure of the regex. So I changed the code and added return 0 if not defined $got; right after copying the second parameter to the $got variable on the 2nd row of the function. So I had sub descend { my $self = shift; my $got = shift; return 0 if not defined $got; ... Then I ran my tests again. This time the test failed and told me exactly which key in my hash was undef. At this point I had two choices: If having and undef there was incorrect then I'd need to fix my code, or if the undef was acceptable I could change my test to be: field => any(undef, re('...')), with the appropriate regex. This expression will only try to run the regex if the value is not undef. A full example of the problem The following script can demonstrate the issue: examples/undef_in_test_deep_regex.pl use Test::More; use Test::Deep; plan tests => 1; my @data = ( { name => 'Foo', }, { name => undef, }, ); cmp_deeply \@data, array_each({ name => re('^.*$') }); I have even opened a ticket with this issue, but I am not sure if my recommended sol
Search HCL Search Reviews Search ISOs Go to Page... LinuxQuestions.org > Forums > Non-*NIX Forums > Programming Use of uninitialized value in pattern match - perl User Name Remember Me? Password Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game. Notices Welcome to LinuxQuestions.org, a friendly and active Linux Community. You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today! Note that registered members see fewer ads, and ContentLink is completely disabled once you log in. Are you new to LinuxQuestions.org? Visit the following links: Site Howto | Site FAQ | Sitemap | Register Now If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here. Having a problem logging in? Please visit this page to clear all LQ-related cookies. Introduction to Linux - A Hands on Guide This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter. For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own. Click Here to receive this Complete Guide absolutely free. Search this Thread 06-26-2011, 01:12 PM #1 casperdaghost Member Registered: Aug 2009 Posts: 349 Rep: Use of uninitialized value in pattern match - perl I want to strip the process name from the hosts - i did it with the code below. I have two questions - is there a more compact way to strip off the process names? usalso i want to get rid of the errors after extracting the hostname. It is complaing about $arry[1]. using my $arry[1] is not allowed. Assigning the slice to a value, as is 'my $sliced_arry = $arry[1]; print $sliced_arry , does no