array and filehandle.

N

Nene

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....
}
 
A

A. Sinan Unur

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 <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
G

Gunnar Hjalmarsson

Nene said:
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?
 
J

Jürgen Exner

Nene said:
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
 
U

Uri Guttman

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
 
J

Jürgen Exner

Uri Guttman said:
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
 
U

Uri Guttman

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
 
E

Eric Pozharski

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
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.
 
K

kath

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?



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

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

# do something

}

close $fh;

--
A. Sinan Unur <[email protected]>
(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...
}
 
J

John W. Krahn

Ben said:
Quoth Ben Morrow said:
{
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top