0

I'm having trouble doing a match for this xml string in perl.

<?xml version="1.0" encoding="UTF-8"?><HttpRemoteException path="/proj/feed/abc" class="java.io.FileNotFoundException" message="/proj/feed/abc: No such file or directory."/>

I want to place a condition on FileNotFoundException like so:

code snippet:

my @lines = qx(@cmdargs);
foreach my $line (@lines) { print "$line"; }

if (my $line =~ m/(FileNotFoundException)/) {
     print "We have an ERROR: $line\n";
}

Error:

Use of uninitialized value in pattern match (m//) at ./tst.pl
5
  • 1
    Have you tried removing the "my"? It might be re-initializing your variable. Commented Sep 22, 2011 at 4:56
  • I did. I edited my question. I tried removing my as well and using use strict and use warnings Commented Sep 22, 2011 at 5:08
  • The following executes properly for me: use strict; use warnings; my $line = '<?xml version="1.0" encoding="UTF-8"?><HttpRemoteException path="/proj/feed/abc" class="java.io.FileNotFoundException" message="/proj/feed/abc: No such file or directory."/>'; if ($line =~ m/(FileNotFoundException)/) { print "We have an ERROR: $line\n"; } Commented Sep 22, 2011 at 5:18
  • thanks, works for me too. something else i need to troubleshoot then. Commented Sep 22, 2011 at 5:25
  • 1
    Yawn. Use an XML library to parse XML. Use an XML library to parse XML. Use an XML library to parse XML. Use an XML library to parse XML. Use an XML library to parse XML. Use an XML library to parse XML. There are several for Perl, I always use XML::LibXML. Commented Sep 22, 2011 at 13:13

2 Answers 2

5

You never assign anything to the variable against which you match (since you create the variable right there inside the if condition), so it doesn't contain what you say it does.

Use use strict; use warnings;!!!

It would have given you a warning. Remove the my.

Sign up to request clarification or add additional context in comments.

Comments

2

You should test $lineinside the foreach loop:

my @lines = qx(@cmdargs);
foreach my $line (@lines) {
    print "$line";
    if ($line =~ m/(FileNotFoundException)/) {
        print "We have an ERROR: $line\n";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.