Professional Web Applications Themes

pass a filehandle then use it in grep{}? - PERL Beginners

this does not work: sub extract{ #retrieve subset of data from a single file #return that data in a list context my $fh = shift; #filehandle reference my creds; local $/ = '<entry>'; creds = grep {/./} $fh; creds } this does work:: sub extract{ #retrieve subset of data from a single file #return that data in a list context my $fh = shift; #filehandle reference my creds; local $/ = '<entry>'; for (<$fh>){ push creds,$_}; creds } the first returns *main::IN the second returns the actual data that i want. i looked at perldoc perlref and the camel book- ...

  1. #1

    Default pass a filehandle then use it in grep{}?

    this does not work:

    sub extract{
    #retrieve subset of data from a single file
    #return that data in a list context

    my $fh = shift; #filehandle reference
    my creds;
    local $/ = '<entry>';
    creds = grep {/./} $fh;
    creds
    }


    this does work::

    sub extract{
    #retrieve subset of data from a single file
    #return that data in a list context

    my $fh = shift; #filehandle reference
    my creds;
    local $/ = '<entry>';
    for (<$fh>){ push creds,$_};
    creds
    }




    the first returns *main::IN
    the second returns the actual data that i want.

    i looked at perldoc perlref and the camel book- tried different ways of
    doing things- but i must be missing something!!!

    I doubt that one is much better than the other for what i want to do,
    but i would like to understand why i can't do it the first way.

    *sigh*


    thanks :)

    willy
    http://www.hackswell.com/corenth

    William Guest

  2. #2

    Default Re: pass a filehandle then use it in grep{}?

    William M West wrote: 

    Try:

    creds = grep {/./} <$fh>;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default RE: pass a filehandle then use it in grep{}?



     
    >
    >Try:
    >
    > creds = grep {/./} <$fh>;
    >
    >--
    >Gunnar Hjalmarsson
    >Email: http://www.gunnar.cc/cgi-bin/contact.pl[/ref]

    ah!! i was so used to that /not/ being the case with a normal
    filehandle that i didn't think to use it with a reference....

    *laugh*

    thank you :) i'm going to go play with Benchmark and see what that
    gives me....




    willy
    http://www.hackswell.com/corenth

    William Guest

  4. #4

    Default Re: pass a filehandle then use it in grep{}?

    William M West wrote: 
    >>
    >> Try:
    >>
    >> creds = grep {/./} <$fh>;[/ref]
    >
    > ah!! i was so used to that /not/ being the case with a normal
    > filehandle[/ref]

    Don't understand. Which syntax(es) are you referring to when saying that?
     

    You're welcome.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  5. #5

    Default RE: pass a filehandle then use it in grep{}?



     
    >>
    >> ah!! i was so used to that /not/ being the case with a normal
    >> filehandle[/ref]
    >
    >Don't understand. Which syntax(es) are you referring to when saying[/ref]
    that?

    my array =grep {-d "$directory\/$_"} readdir <LS2>;

    gives me this error::

    Type of arg 1 to readdir must be HANDLE (not <HANDLE>) at ./subdirs.pl
    line 30, near "<LS2>;"
    Execution of ./subdirs.pl aborted due to compilation errors.


    but of course that's NOT the same as :
    my array =grep {/foo/} <LS2>;


    which is fine- my careless reading of my other code got the misuse of <>
    stuck in my head- that kept me from seeing the proper solution.



    willy
    http://www.hackswell.com/corenth
    William Guest

  6. #6

    Default Re: pass a filehandle then use it in grep{}?

    William M West wrote: 
    >>
    >> Don't understand. Which syntax(es) are you referring to when
    >> saying that?[/ref]
    >
    > my array =grep {-d "$directory\/$_"} readdir <LS2>;
    >
    > gives me this error::
    >
    > Type of arg 1 to readdir must be HANDLE (not <HANDLE>) at
    > ./subdirs.pl line 30, near "<LS2>;"
    > Execution of ./subdirs.pl aborted due to compilation errors.
    >
    > but of course that's NOT the same as :
    > my array =grep {/foo/} <LS2>;[/ref]

    Right. readdir() expects a DIRHANDLE as the argument, while grep()
    expects a LIST as the second argument.
     

    Make it a habit to study the docs for the functions you are using:

    perldoc -f grep
    perldoc -f readdir

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  7. #7

    Default RE: pass a filehandle then use it in grep{}?


     
    >
    >Make it a habit to study the docs for the functions you are using:
    >
    > perldoc -f grep
    > perldoc -f readdir[/ref]


    oddly- the grep doentation in perldoc doesn't even mention
    filehandles. readdir does show proper use though- when i looked up grep
    in the perldocs, the issue remained unclear to me. But, of course, it
    was my self induced "blindness" that kept me from seeing the solution.

    Now, a <FILEHANDLE> might be seen in a list context- but connecting
    that to grep {code} list; was just too much for my little brain to
    handle at that time *laugh*
     



    willy
    http://www.hackswell.com/corenth

    William Guest

  8. #8

    Default Re: pass a filehandle then use it in grep{}?

    William M West wrote: 
    >
    > oddly- the grep doentation in perldoc doesn't even mention
    > filehandles. readdir does show proper use though- when i looked up
    > grep in the perldocs, the issue remained unclear to me.[/ref]

    The grep() doentation doesn't mention filehandles because the
    purpose of grep() is to deal with a LIST, irrespective of how the LIST
    was generated. A filehandle within <> symbols may return a list in
    list context. An array may as well, as does the plain
    ('one', 'two', 'three') notation or any function or operator that
    returns a list...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  9. #9

    Default Re: pass a filehandle then use it in grep{}?

    West, William M wrote: 

    The expression "readdir <LS2>;" is the same as "readdir readline LS2;".
    Whenever you see a FILEHANDLE delimited by < and > it the same as using
    readline with that FILEHANDLE.

    perldoc -f readline


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  10. #10

    Default Re: pass a filehandle then use it in grep{}?

    West, William M wrote: 

    readdir does not use a filehandle and therefore does not show the proper use
    of them. The opendir/readdir/rewinddir/seekdir/telldir/closedir functions all
    use directory handles and only directory handles.


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. closed filehandle
    By Sam in forum PERL Miscellaneous
    Replies: 18
    Last Post: September 15th, 05:54 PM
  2. pass cmd line file glob to grep for readdir
    By qanda in forum PERL Miscellaneous
    Replies: 9
    Last Post: September 11th, 01:53 PM
  3. FILEHANDLE to print to nothing
    By Dan Muey in forum PERL Beginners
    Replies: 1
    Last Post: August 25th, 09:39 PM
  4. first.pl | second.pl with ARGV filehandle ?
    By Uri Guttman in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 3rd, 02:51 AM
  5. Using filehandle many times
    By Thens in forum PERL Miscellaneous
    Replies: 0
    Last Post: June 30th, 01:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139