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

Hi greetings to everyone.I am here to seek the wisdom of monks,here is my issue.In this this module(prerequest_collect_files_db_url) i have to create 2 text files and in next module(prerequest2.pl) i have to compare both text files.But text files are created only at the end of second module.so i cant find the difference.can some one please help me on this.I need those text files before it calls "prerequest2.pl"

sub prerequest_collect_files_db_url { open(FILE, "C:/Users/jeyakuma/Desktop/shipping project/input/input.txt +"); open HTML1,">C:/Users/jeyakuma/Desktop/shipping project/url processin +g/competitors_in_url.txt"; while(<FILE>) { chomp; $url=$_; foreach ($url) { do 'C:/Users/jeyakuma/Desktop/perl/xpathconfiguration.pl'; &domain_check(); $x="$competitor.html"; print HTML1 "$x\n"; } } open HTML2,">C:/Users/jeyakuma/Desktop/shipping project/url processing +/competitors_in_directory.txt"; opendir(DIR, "C:/Users/jeyakuma/Desktop/shipping project/database"); @files = grep(/\.html$/,readdir(DIR)); foreach $file (@files) { print HTML2"$file\n"; } } closedir dir; close HTML1; close HTML2; close FILE; do "C:/Users/jeyakuma/Desktop/perl/prerequest2.pl"; &find_missing_files();
sub find_missing_files { use strict; use warnings; use List::Compare; open F, "<C:/Users/jeyakuma/Desktop/shipping project/url processing/co +mpetitors_in_url.txt" ; open S, "<C:/Users/jeyakuma/Desktop/shipping project/url processing/co +mpetitors_in_directory.txt" ; my @a=<F>; my @b=<S>; my $lc = List::Compare->new(\@a,\@b); my @intersection = $lc->get_intersection; my @firstonly = $lc->get_unique; my @secondonly = $lc->get_complement; #print "Common Items:\n"."@intersection"."\n"; print "Please configure and create dump in Databse for this competitor +s !!\n"."\n@firstonly"."\n"; print "please give the input URL for the following competitiors !!\n". +"\n@secondonly"."\n"; } }

Expected out put at the end of "prerequest_collect_files_db_url"

text1

text2

expected o/p after prerequest.pl

file missed in dir are a,b,c

file missed in text

current result after prerequest.pl

text1

text2

Replies are listed 'Best First'.
Re: text files are printed after the end of second module
by Corion (Patriarch) on Jul 06, 2014 at 17:18 UTC
    sub prerequest_collect_files_db_url { ... }

    You never call that subroutine.

    Maybe if you call that subroutine, it will actually create the files you want.

      hi thanks for ur reply but this sub"prerequest_collect_files_db_url" is responsible for creating text files and after creating the text files i have to compare that via prerequest.pl.but after completing prerequest.pl only text files are creating so i have to run the code twice to see the result

        At least in the code you have shown so far, you never call the subroutine.

Re: text files are printed after the end of second module
by Laurent_R (Canon) on Jul 06, 2014 at 21:53 UTC
    You would do yourself a great favor if you followed the following guidelines:
    • indent your code consistently and properly;
    • use strict; (you are doing it for the second script, but apparently not for the first)
    • use warnings; (you are doing it for the second script, but apparently not for the first)
    • use the 3-argument syntax for opening files and directories (see open) and check if the opening was successful;
    • use lexical filehandles;
    • use the modern syntax for calling subroutines:
    • domain_check();
      and not:
      &domain_check();
    • remove extra vertical spaces that are not needed;
    • close your files when they are no longer needed;
    • lay-out your question and code snippets in a clearer fashion, using code tags not only for code, but also for displaying program output.

    Nothing of the above is absolutely necessary, but these things are more important than being necessary, they are essential (i.e. they are part of the essence of good programming). I am sorry to say that, but I frankly don't feel like spending the time to go through such messy code (and I would certainly not hire a programmer who would show me such a sloppy work). You might find my comments so far to be harsh, but they are really meant to help you (and to help us, poor monks, helping you). I am not being extremist or fanatic, quite to the contrary. If I had decided to follow Damian Conway's Perl Best Practices, I would have at least three dozen additional recommendations, so be happy that it is me, and not TheDamian, answering your post. (Just in case, I am just jokingly saying that I might have been much harsher, this is by no means, and should not be understood, construed or interpreted as, a critique on Damian's guidelines.)

    Having said that, I am not a bad guy, I am actually much nicer than you might think at this point and, despite everything I said so far, I took a bit of time to look a bit through your code, and I have a couple of additional comments.

    You should probably not use the do function to include external Perl code, this is quasi obsolete. Take a look at the require and use functions and other documentation on Perl modules.

    This piece of code:

    $url=$_; foreach ($url) { #...
    does not make much sense to me. The $_ scalar variable contains one single data item (in this case, a text line), and the $url scalar variable also contains one single data item. Why would you want to use foreach to loop on a single data item?

    As for your precise question, I am sorry, I am unable to answer because your code is incomplete, you haven't shown a sample of the data files you are reading, we don't know what the scripts you are calling with the do function are doing, and your question is far from being clear. But if you do everything that I suggested above, you might actually see what the problem is and find a solution to it. And even if this is not sufficient, well, then post your cleaned-up code, I am sure many more monks (including myself) will feel like wanting to help you further.

      You might find my comments so far to be harsh, but they are really meant to help you (and to help us, poor monks, helping you). I am not being fanatic.
      You're a nice guy! What the OP posted was a perfect illustration of "write-only Perl". But can Perl itself be blamed for it? I don't think so.

      Hi Thanks for your suggestions and I take all your feed backs and try to improve myself.Thanks for your time

Re: text files are printed after the end of second module
by Anonymous Monk on Jul 06, 2014 at 17:26 UTC

    Your code seems a bit incomplete, since I don't see where prerequest_collect_files_db_url is being called, but I'll assume it's called before you do prerequest2.pl?

    The thing that stands out to me is that you close your filehandles outside of the sub, why? I suggest you close your files right after you're done with them. Also, you may want to look into lexical filehandles, which aren't global and get closed when they go out of scope (in case you forget to do it explicitly, which you always should anyways). For example: open my $filehandle, "<", "filename.txt" or die $!; my @input = <$filehandle>; close $filehandle;

      thanks for your reply.yes I call "prerequest_collect_files_db_url" first after creating text files in this and I will compare that in prerequest2.pl. When I close there itself i am getting only one line in text files

        Your original post implied that the files were empty when you call prerequest2.pl, but if they have some content then the error situation is actually completely different. Please see "How do I post a question effectively?" and follow the suggestions there (such as providing sample input and expected output) so your posts are more helpful to those providing help.

        How many lines are there in your input? Have you tested your conditions and other parts of the code (xpathconfiguration.pl, domain_check(), and the grep) to be working? I'd suggest printing some debug output in your innermost loops to see what actually gets written to the file. Also, see Basic debugging checklist

Re: text files are printed after the end of second module
by Anonymous Monk on Jul 07, 2014 at 07:43 UTC

    Hi greetings to everyone.I am here to seek the wisdom of monks,here is my issue.

    This seems familiar, maybe you missed my reply from yesterday Re: loop exits after printing the file

    do is not how perl programmers make modules, or use modules; modules should only define/create subroutines, they shouldn't do anything on their own ... there should be no code outside of subroutines, subroutines should be self contained

    Making a real module is easy if you follow these examples: Re: loop exits after printing the file, zentara package/module tutorial

    If you write code this way and create real modules, with subroutines that take arguments and return values, it will be easy to figure out why you need to call your program twice for the files to be created in time to do the comparison

    MakeFilesOrDie( $these, $files ); CompareFilesOrDie( $these, $files );

    What you have posted is like a magic act

    $url=$_; &domain_check(); $x="$competitor.html";

    What does domain_check do? Where does $competitor come from?

    Why doesn't domain_check take arguments?

    Why doesn't domain_check return values?

    Maybe it should be  my $competitor = domain_check( $url ); but its impossible to know, your posted program has many missing pieces...

    Its like a buying stuff at a store, the buyer doesn't reach into the cash register to pay for items and get change, the buyer gives money to cashier, the cashier gives change back or ask for more money

    my $change = gimmeChange( '20USD', 'milk','cookies' ); sub gimmeChange { my( $payment , @items ) = @_; my $price = gimmePrice( @items ); my $diff = $payment - $price; if( $payment < $price ){ warn "You don't have enough money, you need $diff"; } return $diff; }

      Hi all thanks for your reply and suggestions I started to learn perl before 1 week only and really I don't know how to write a code clearly .I will take all your suggestions and try to improve myself.Thanks for you time and suggestions.