namisha has asked for the wisdom of the Perl Monks concerning the following question:

I have two xml outputs and from that output need to compare the datasize. if matches pass if not matches then fails. example: File 1:
response message: containerType="TRWF_DATA_DT_FIELD_LIST" r +esponseTypeNum="TRWF_TRDM_RPT_SOLICITED_RESP" domainType="TRWF_TRDM_ +DMT_MARKET_BY_PRICE dataSize:5000
File 2:
response message: containerType="TRWF_DATA_DT_FIELD_LIST" r +esponseTypeNum="TRWF_TRDM_RPT_SOLICITED_RESP" domainType="TRWF_TRDM_ +DMT_MARKET_BY_PRICE dataSize:5000
if dataSize does not match it is fail otherwise pass. How can I write perl script to verify this

Replies are listed 'Best First'.
Re: Perl script
by NetWallah (Canon) on Feb 07, 2017 at 04:50 UTC
    Hello, and welcome to the Monastary!

    Please see Markup in the Monastery to learn how to format your message appropriately.

    <UPDATE>:Thank you namisha for updating your post and fixing formatting.
    For future reference, please UPDATE you post to indicate you have done so.
    This helps readers make sense of comments, like this one, that are posted below your original post. </UPDATE>

    The examples you posted do not look like XML , and they have unmatched quotes.

    You can write a script to verify the "datasize" by learning how to open and read files in perl, in Tutorials.
    Learn regular expressions to extract the "datasize".

    Here is a sample regular expression to get you started:

    my $data = "... domainType="TRWF_TRDM_DMT_MARKET_BY_PRICE dataSize:500 +0"; my ($datasize) = $data=~/dataSize:(\d+)/;
    If your files are proper XML, you can use one of several XML libraries on cpan.

    If you get into trouble with coding, please post your attempt, and problem here .. we will be glad to help.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re: Perl script
by AnomalousMonk (Archbishop) on Feb 07, 2017 at 19:10 UTC
Re: Perl script
by madtoperl (Hermit) on Feb 07, 2017 at 10:34 UTC
    since your output is already in two xml files, let's say File 1 is one.xml and File 2 is two.xml.
    You can do the below. Also there are other ways also available eg. using File::Stat module as well.
    #!/usr/bin/perl use strict; use warnings; my $filesize1 = 0; my $filesize2 = 0; if (-e 'one.xml'){ $filesize1 = -s 'one.xml'; } if ( -e 'two.xml'){ $filesize2 = -s 'two.xml'; } if ( $filesize1 == $filesize2) { print "File sizes are equal\n"; }else{ print "File sizes are not equal\n"; }

      OP is not trying to compare the size of two XML files, but the value of dataSize within two XML files. While it doesn't address this problem your example should also check that the files exist before allocating a size to them, e.g. my $filesize2 = 0;.

      Checking -e first is a bad idea, as it leads to race conditions (the file might be deleted between the -e and -s). Just call -s. If it gives undef, the file doesn't exist.

      The OP is so vague that I'm not sure whether "datasize" is a field in the XML or the length of some part of the XML.