in reply to Re: perl regular expressions
in thread perl regular expressions

I guess dumper that you specified above is to display the content of the file..But i need to check if the string is present in the file if not thrown an error. I need the script to check if the bandwidth is 100, link is http://www.espn.com .If not the the script should throw an error

Replies are listed 'Best First'.
Re^3: perl regular expressions
by AppleFritter (Vicar) on Jun 23, 2014 at 23:41 UTC
    Yes. You'd have to read the data from your file and then use the undump function from Data::Undump on it -- or something similar. Alternatively, you could perhaps roll your own parser; whether that's worth the effort depends on how much processing you intend to do with your data.

      Sorry ,I didn't understand what you said. Could you please be more specific ? My requirement is i need to parse a file for certain strings and if they are not present, the script should throw an error. data::dumper is used to read the file and print the contents of the file.But I'd like to verify if the file contains some specific strings and throw an error if the strings are not present.

        What I meant was that you'd have to read the data and then process it. Consider the following snippets. The first produces a data file:

        #!usr/bin/perl use feature qw/say/; use Data::Dumper; $Data::Dumper::Terse = 1; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; say Dumper($hash);

        You'll want to invoke this as perl script.pl >data.txt or so, BTW. Here's what the resulting data file will look like:

        { 'testing' => { 'link' => 'http://www.espn.com', 'bandwidth' => '100', 'r' => '2' } }

        This in turn can be fed to another script again, perhaps something along the following lines:

        #!/usr/bin/perl use feature qw(say); use Data::Undump; undef $/; $data = <>; $undump = undump($data); die "'testing' not found!" unless($undump->{'testing'}); say $undump->{'testing'}->{'link'} // (die "'link' not found!"); say $undump->{'testing'}->{'bandwidth'} // (die "'bandwidth not found! +");

        This should be invoked as perl script.pl <data.txt; it'll read in the data in one big chunk, undump it, verify that the testing key exists (and raise an error otherwise), and print the link and bandwidth values associated with it, again raising an error if they don't exist (by abusing the // defined-or operator, which I'm not convinced is considered good style).

        Playing around with this, I've found that Data::Undump can be fairly fussy about exactly what the data you feed it should look like (which is perhaps what the author meant when they aluded to it being an "early release"), so depending on how much control you have over the processes creating these data files, it may not be suitable for you. Certainly it does not seem like a very robust module (in the Postelian sense), and the cleanest solution might be to write a proper parse using Parse::RecDescent or so. Whether that's worth the effort is perhaps another question.

      First of all,Thank you so much for explaining in detail.Appreciate it.

      Is there a way to combine these two scripts into one single script

      #!usr/bin/perl use feature qw/say/; use Data::Dumper; $Data::Dumper::Terse = 1; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; say Dumper($hash); undef $/; $data = <>; $undump = undump($data); die "'testing' not found!" unless($undump->{'testing'}); say $undump->{'testing'}->{'link'} // (die "'link' not found!"); say $undump->{'testing'}->{'bandwidth'} // (die "'bandwidth not found! +");
      I tried to do it ,but looks likes the 2nd part didn't work. Any suggestions please ?

        You're welcome!

        Yes, there is a way to combine them into one. Add a use Data::Undump, and then change the the part between say Dumper($hash) and $data = <> so that you're not printing to STDOUT and reading from STDIN anymore:

        #!usr/bin/perl use feature qw/say/; use Data::Dumper; use Data::Undump; $Data::Dumper::Terse = 1; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; $data = Dumper($hash); $undump = undump($data); die "'testing' not found!" unless($undump->{'testing'}); say $undump->{'testing'}->{'link'} // (die "'link' not found!"); say $undump->{'testing'}->{'bandwidth'} // (die "'bandwidth not found! +");

        But of course now you're dumping and undumping your data in the same script, which is basically a no-op. So you could further simplify this to the following:

        #!usr/bin/perl use feature qw/say/; $hash = { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; die "'testing' not found!" unless($hash->{'testing'}); say $hash->{'testing'}->{'link'} // (die "'link' not found!"); say $hash->{'testing'}->{'bandwidth'} // (die "'bandwidth not found!") +;

        But that's got little to do with you original question anymore.

      Thanks a ton. Appreciate it !!
      Is there a way to read the hash value from a txt file ?
        Well, yes. That's what Data::Undump is for, for instance. As shown above, it does the job, too, though it appears it is still a bit fussy about how that file must be formatted. (Anything not straight out of Data::Dumper might cause problems.)
      Hi can you please help me out with reading the data from file. { 'testing' => { 'link' => "http://www.espn.com", 'bandwidth' => "100", 'r' => "2", }, }; instead of specifying the above value in the code. i want to read it from the text file..I tried but couldn't do it.

        I'm not sure what I can add to everything that's been said above already, by me and others. I sympathize with your problems, but I can't and won't write all your code for you.

        (But do feel free to inquire about my rates for paid consultant work. :P)