#!/usr/bin/perl

use strict;
use warnings;

#######
#  regex_issues.pl
#
#######

my $filename = "salon.xml";
my $pattern;
my $line_number = 0;
my $found;
my @saved_xml;
my $whole_shebang_as_one_scalar;
my $dont_want = "<item>";

#this loop is used to read in the file, and needs to be turned on for my regex problems down below.
while (1) {
# Get in, save the file, get out.
open(INPUT, '<', $filename) or die $!;
@saved_xml = <INPUT>;
close INPUT;

#save to scalar
$whole_shebang_as_one_scalar = join("", @saved_xml);

#print "\@saved_xml: ";
#<STDIN>;
#print @saved_xml;
#<STDIN>;
#print "\$whole_shebang_as_one_scalar : ";
#<STDIN>;
#print $whole_shebang_as_one_scalar;
#<STDIN>;
last;
} #close this while loop

# this loop is just for testing any old regular expression
while (0) {

print "Type a pattern: \n";
chomp ($pattern = <STDIN>);

$found = 0;

if ($whole_shebang_as_one_scalar =~ m/$pattern/) {
	print "FOUND!\n";
	print "\$1 is $1\n" if $1;
	print "\$2 is $2\n" if $2;
	print "\$3 is $3\n" if $3;
	print "\$4 is $4\n" if $4;
	print "\$5 is $5\n" if $5;
	
}
else {
	print "FROWND (NOT FOUND)\n";
}

} #finish while loop


# test regex case, where it works
while (0) {
my $string = "neighbarkol\nkofoolamalabamba";

if ($string =~ m/(bar(.|\s)*)(foo)/) {
	print "CHECK 1: YES janice! The raptor has been quarantined! His name is '$1!'\n\n\n";
	print "$1\n\n$2\n\n$3\n\n$4\n";
}
else {
	print "CHECK 1: THE RAPTOR HAS ESCAPED\n";
}

<STDIN>;
if ($string =~ /(?<!$1)foo/) {
	print "CHECK 2: YES janice! There is NOT a bar before this foo!\n";
}
else {
	print "CHECK 2: Negations, janice! We can't go here!\n";
}
<STDIN>;

last;
} #finish another while loop

#applied regex case, where it fails
while (1) {
if ($whole_shebang_as_one_scalar =~ m/((.|\s)*<item>(.|\s)*)<title>(.|\s)*<title>/) {
	print "CHECK 1: YES janice! The raptor has been quarantined! His name is '$1!'\n";
	<STDIN>;
	print "Ok, we'll print them all:\n\$1:\t$1\n\$2:\t$2\n\$3:\t$3\n";
}
else {
	print "DANGER AHEAD\n";
}

<STDIN>;
if ($whole_shebang_as_one_scalar =~ m/(?<!$1)/) {
	print "CHECK 2: YES janice!\n";
}
else {
	print "CHECK 2: for FEAR, janice! Think of the RAPTOR!\n";
}
<STDIN>;
print "$1\n$2\n$3";
last;
} #another script doomed by a finished while loop