CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
is there a better way than grep ?

 



gerble1000
User

Jun 1, 2012, 6:12 AM

Post #1 of 5 (1564 views)
is there a better way than grep ? Can't Post

im using mechanise to login and do things.


Code
$mech->save_content( 'test.txt' ); 

open(FILE,"test.txt");
if (grep{/word_to_look_for/} <FILE>){
close FILE;
"run some code";
}else{
close FILE;
"run some code";
}


is there a better way to do this .
i want to check the returned page from mechanize and see if it contains a sentence then i want an if/else statement


BillKSmith
Veteran

Jun 1, 2012, 8:15 AM

Post #2 of 5 (1560 views)
Re: [gerble1000] is there a better way than grep ? [In reply to] Can't Post

Your approach is not bad. I could suggest several others, but each would be a trade-off among several issues.

  • Conforming to coding standards


  • Time required to write and test the perl code.


  • Ease in making possible future changes.


  • Memory required.


  • Execution Speed.


  • Time that file is open.


  • Use of an explicit function (e.g. any in List::MoreUtils)

  • Good Luck,
    Bill


    FishMonger
    Veteran / Moderator

    Jun 1, 2012, 8:27 AM

    Post #3 of 5 (1557 views)
    Re: [gerble1000] is there a better way than grep ? [In reply to] Can't Post

    It would be more efficient to work with the data in memory rather than saving the content to a file that you then need to open and read. Disk I/O is generally the slowest aspect of a given process.


    gerble1000
    User

    Jun 1, 2012, 8:37 AM

    Post #4 of 5 (1554 views)
    Re: [FishMonger] is there a better way than grep ? [In reply to] Can't Post

    would you have an example please :)
    and do you think grep is the best way of doing this


    FishMonger
    Veteran / Moderator

    Jun 1, 2012, 8:46 AM

    Post #5 of 5 (1552 views)
    Re: [gerble1000] is there a better way than grep ? [In reply to] Can't Post

    I have never worked with WWW::Mechanize but glancing over its documentation I see a content method that could be used to save the content to a scalar and if you do that, then I'd use the index function instead of grep to test for the existence of what you need.

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives