array and filehandle.

Discussion in 'Perl Misc' started by Nene, May 20, 2009.

  1. Nene

    Nene Guest

    Hi,

    I would like to turn my array into a filehandle like:

    open(FILE, @array);

    is that possible?

    So I can do:

    while (my $line = <FILE>) {
    my code goes here....
    }
     
    Nene, May 20, 2009
    #1
    1. Advertising

  2. Nene <> wrote in news:e13780b2-d915-4bc1-9cc0-
    :

    > I would like to turn my array into a filehandle like:
    >
    > open(FILE, @array);
    >
    > is that possible?


    No, that is not possible. Your question is not very clear.

    Do you have an array of filenames and you want to open the corresponding
    file for each in turn? Or, do you have a filename and you want to read
    the contents of the file in to an array?

    > So I can do:
    >
    > while (my $line = <FILE>) {
    > my code goes here....
    > }


    open my $fh, '<', 'filename'
    or die "Cannot open 'filename': $!";

    while ( my $line = <$fh> ) {

    # do something

    }

    close $fh;


    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, May 20, 2009
    #2
    1. Advertising

  3. Nene wrote:
    > I would like to turn my array into a filehandle like:
    >
    > open(FILE, @array);
    >
    > is that possible?
    >
    > So I can do:
    >
    > while (my $line = <FILE>) {
    > my code goes here....
    > }


    If you are willing to 'convert' the array to a scalar, you can do:

    perl -e '
    @array = qw/abc def ghi/;
    $scalar = join("\n", @array)."\n";
    open FH, "<", \$scalar;
    print while <FH>;
    '
    abc
    def
    ghi

    But why not just loop over the array?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 20, 2009
    #3
  4. Nene <> wrote:
    >I would like to turn my array into a filehandle like:
    >
    >open(FILE, @array);
    >
    >is that possible?


    I haven't tried it, but if the first element of the array is the mode
    and the second the file name, then I don't see why there should be a
    problem. At least I would assume that the function call semantics for
    Perl's build-in functions are the same as for user-defined functions,
    i.e. both flatten the argument list.

    >So I can do:
    >
    >while (my $line = <FILE>) {
    >my code goes here....
    >}


    You can always do that, no matter how you opened the file (well, as long
    as you open it for reading, but I guess that's obvious).

    jue
     
    Jürgen Exner, May 20, 2009
    #4
  5. Nene

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> Nene <> wrote:
    >> I would like to turn my array into a filehandle like:
    >>
    >> open(FILE, @array);
    >>
    >> is that possible?


    JE> I haven't tried it, but if the first element of the array is the mode
    JE> and the second the file name, then I don't see why there should be a
    JE> problem. At least I would assume that the function call semantics for
    JE> Perl's build-in functions are the same as for user-defined functions,
    JE> i.e. both flatten the argument list.

    nope. remember many functions can take an array as the first arg so they
    actually parse things with prototypes. you can find out the prototype of
    any builtin with the function prototype. only those funcs that take a
    final list do any slurping (splice, push, etc.) from the args list.

    perl -le 'print prototype "CORE::eek:pen"'
    *;$@

    so that means open takes a handle and then an optional scalar (the mode
    or file name) and then slurps a list. assuming i interpret the prototype
    correctly which could be wrong since i rarely use them. actually my
    first real use recently was in a test script where i needed to override
    sysread/write with a sub to force an error.

    but as others have said, the OP's question is not clear. is it an array
    of file names or lines? where is the mode?

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 20, 2009
    #5
  6. "Uri Guttman" <> wrote:
    >>>>>> "JE" == Jürgen Exner <> writes:

    >
    > JE> Nene <> wrote:
    > >> I would like to turn my array into a filehandle like:
    > >>
    > >> open(FILE, @array);
    > >>
    > >> is that possible?

    >
    > JE> I haven't tried it, but if the first element of the array is the mode
    > JE> and the second the file name, then I don't see why there should be a
    > JE> problem. At least I would assume that the function call semantics for
    > JE> Perl's build-in functions are the same as for user-defined functions,
    > JE> i.e. both flatten the argument list.
    >
    >nope. remember many functions can take an array as the first arg so they
    >actually parse things with prototypes. you can find out the prototype of
    >any builtin with the function prototype. only those funcs that take a
    >final list do any slurping (splice, push, etc.) from the args list.


    Ah, of course, you right.

    Thanks

    jue
     
    Jürgen Exner, May 21, 2009
    #6
  7. Nene

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth "Uri Guttman" <>:
    >>
    >> nope. remember many functions can take an array as the first arg so they
    >> actually parse things with prototypes. you can find out the prototype of
    >> any builtin with the function prototype. only those funcs that take a
    >> final list do any slurping (splice, push, etc.) from the args list.


    BM> It's important to realise that all the builtins are actually parsed
    BM> specially, and many cannot be represented by prototypes, even some of
    BM> those that claim to have them. The most obvious example is 'eof' (which
    BM> cannot be reimplemented in Perl at all), but there are others like
    BM> 'stat' and 'chdir' which take a filehandle or a string and get hints
    BM> from the parser about which was passed which a user function cannot
    BM> access.

    sure. and also stat and the -X functions can work with _ as an arg. who
    knows how perl parses that! one of the joys perl6 will have is a proper
    parsable grammar (and a full perl6 parser accessible from perl6 and
    written in perl6 grammar!).

    but the main point is you can rarely pass an array to a builtin and have
    it use those values for args.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 21, 2009
    #7
  8. On 2009-05-20, Uri Guttman <> wrote:
    >>>>>> "JE" == Jürgen Exner <> writes:

    >
    > JE> Nene <> wrote:
    > >> I would like to turn my array into a filehandle like:
    > >>
    > >> open(FILE, @array);
    > >>
    > >> is that possible?

    >
    > JE> I haven't tried it, but if the first element of the array is the mode
    > JE> and the second the file name, then I don't see why there should be a
    > JE> problem. At least I would assume that the function call semantics for
    > JE> Perl's build-in functions are the same as for user-defined functions,
    > JE> i.e. both flatten the argument list.


    And? Noone actually tried?

    $ perl -wle '@xy = qw( > /dev/null ); open $fh, @xy or die $!'
    Name "main::fh" used only once: possible typo at -e line 1.
    No such file or directory at -e line 1.

    $ perl -wle '@xy = qw( > ); open $fh, @xy or die $!'
    Name "main::fh" used only once: possible typo at -e line 1.
    No such file or directory at -e line 1.

    $ perl -wle 'open $fh, ">" or die $!'
    Name "main::fh" used only once: possible typo at -e line 1.
    No such file or directory at -e line 1.

    > nope. remember many functions can take an array as the first arg so they
    > actually parse things with prototypes. you can find out the prototype of
    > any builtin with the function prototype. only those funcs that take a
    > final list do any slurping (splice, push, etc.) from the args list.
    >
    > perl -le 'print prototype "CORE::eek:pen"'
    > *;$@
    >
    > so that means open takes a handle and then an optional scalar (the mode
    > or file name) and then slurps a list.


    Not exactly. That C<(*)> represents typeglob. And looking in
    F<perlsub>, it can bite.

    > assuming i interpret the prototype correctly which could be wrong
    > since i rarely use them.


    Only perl can parse Perl.


    *CUT*

    Looking at code above I've got to crazy conclusion that there are some
    (at least 2) implementations of B<open>. And that's the compiler what
    decides what route to go. And since it can't get it, it falls back at
    first one, which is C<open FILEHANDLE,EXPR>. Then this B<open> tries to
    open empty filename. And obviously fails.

    Maybe someone with deeper insight could comment?

    p.s. Actually that idea is really crazy, because it's wrong

    $ perl -wle '@xy = qw( /dev/null ); open $fh, @xy or die $!'
    Name "main::fh" used only once: possible typo at -e line 1.
    No such file or directory at -e line 1.


    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, May 21, 2009
    #8
  9. Nene

    kath Guest

    On May 21, 3:35 am, "A. Sinan Unur" <> wrote:
    > Nene <> wrote in news:e13780b2-d915-4bc1-9cc0-
    > :
    >
    > > I would like to turn my array into a filehandle like:

    >
    > > open(FILE, @array);

    >
    > > is that possible?

    >
    > No, that is not possible. Your question is not very clear.
    >
    > Do you have an array of filenames and you want to open the corresponding
    > file for each in turn? Or, do you have a filename and you want to read
    > the contents of the file in to an array?
    >
    > > So I can do:

    >
    > > while (my $line = <FILE>) {
    > > my code goes here....
    > > }

    >
    > open my $fh, '<', 'filename'
    > or die "Cannot open 'filename': $!";
    >
    > while ( my $line = <$fh> ) {
    >
    > # do something
    >
    > }
    >
    > close $fh;
    >
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    >
    > comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clpmisc/


    Hi,
    If you have filenames in a array and want to read sequentially, then
    you can do it in following manner. The default <> operator will open
    each file(s) in @ARGV.

    @ARGV = qw(file1 file2 file3);
    while(<>){
    #do someting...
    }
     
    kath, May 21, 2009
    #9
  10. Ben Morrow wrote:
    > Quoth Ben Morrow <>:
    >> {
    >> package IO::Fake::FromArray;
    >>
    >> use overload "<>" => sub {
    >> my ($self) = @_;
    >>
    >> @$self or return;
    >>
    >> if (wantarray) {
    >> my @tmp = @$self;
    >> @$self = ();
    >> return @tmp;
    >> }

    >
    > @$self or return;
    > wantarray and return delete @{$self}[0..$#$self];
    > return shift @$self;
    >
    > would be both neater and faster, of course. I knew there must be an
    > idiom for 'clear and return this array', I just couldn't think of it :).


    wantarray and return splice @$self;



    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, May 21, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dnrg
    Replies:
    5
    Views:
    135
    Matija Papec
    Jun 26, 2003
  2. Vsevolod Afanassiev

    How to put a filehandle into a hash array?

    Vsevolod Afanassiev, Jul 3, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    99
    Tad McClellan
    Jul 3, 2003
  3. Andrew Hamm

    Archive::Tar and write to an open filehandle

    Andrew Hamm, Oct 9, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    206
    Jens M. Felderhoff
    Oct 9, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    145
    PerlFAQ Server
    Jan 22, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    181
    PerlFAQ Server
    Mar 10, 2011
Loading...

Share This Page