#!/usr/bin/perl

# ./xmltostring.pl output.xml

use strict;
use warnings;

sub parse_xml {
    my ($file_path) = @_;
    open(my $fh, '<', $file_path) or die "Não foi possível abrir o arquivo '$file_path' $!";
    my $content = do { local $/; <$fh> };
    close($fh);
    
    my @categories;
    my @stack;
    my $current_category = '';
    
    foreach my $line (split /\n/, $content) {
        $line =~ s/^\s+|\s+$//g;  # trim
        if ($line =~ /^<Category Assigned="1">(.*?)$/) {
            my $category_name = $1;
            if (@stack) {
                $current_category = join('/', @stack, $category_name); #separador de tags filhos
            } else {
                $current_category = $category_name;
            }
            push @categories, $current_category;
            push @stack, $category_name;
        } elsif ($line =~ /^<\/Category>/) {
            pop @stack if @stack;
        }
    }
    
    return join(', ', @categories);
}

if (@ARGV != 1) {
    print "Uso: perl file.pl input.xml\n";
} else {
    my $file_path = $ARGV[0];
    my $result = parse_xml($file_path);
    print "'$result'\n";
}
