Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > array and filehandle.

Reply
Thread Tools

array and filehandle.

 
 
Nene
Guest
Posts: n/a
 
      05-20-2009
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....
}
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-20-2009
Nene <(E-Mail Removed)> wrote in news:e13780b2-d915-4bc1-9cc0-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-20-2009
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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-20-2009
Nene <(E-Mail Removed)> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-20-2009
>>>>> "JE" == Jürgen Exner <(E-Mail Removed)> writes:

JE> Nene <(E-Mail Removed)> 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: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 ------ (E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-20-2009
"Uri Guttman" <(E-Mail Removed)> wrote:
>>>>>> "JE" == Jürgen Exner <(E-Mail Removed)> writes:

>
> JE> Nene <(E-Mail Removed)> 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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-21-2009
>>>>> "BM" == Ben Morrow <(E-Mail Removed)> writes:

BM> Quoth "Uri Guttman" <(E-Mail Removed)>:
>>
>> 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 ------ (E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
Eric Pozharski
Guest
Posts: n/a
 
      05-21-2009
On 2009-05-20, Uri Guttman <(E-Mail Removed)> wrote:
>>>>>> "JE" == Jürgen Exner <(E-Mail Removed)> writes:

>
> JE> Nene <(E-Mail Removed)> 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: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
 
Reply With Quote
 
kath
Guest
Posts: n/a
 
      05-21-2009
On May 21, 3:35 am, "A. Sinan Unur" <(E-Mail Removed)> wrote:
> Nene <(E-Mail Removed)> wrote in news:e13780b2-d915-4bc1-9cc0-
> (E-Mail Removed):
>
> > 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 <(E-Mail Removed)>
> (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...
}
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-21-2009
Ben Morrow wrote:
> Quoth Ben Morrow <(E-Mail Removed)>:
>> {
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Find if elements of one array are present in the other and return a boolean array Shalini C++ 2 01-09-2004 06:13 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments