473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

key as filehandle error

I'm trying to use a hash key as a filehandle like so.

#!/usr/local/bin/perl
use strict;

my %buf = (
'F00' => 'foo.dat'
);

&open_files();

exit 0;

sub open_files {
while(my($k,$v)=each(%buf)) {
open ($k, ">$v") || die $!;
}
}
But, I get the following error:
Can't use string ("F00") as a symbol ref while "strict refs" in use at
../try.pl line 14.

I've tried varouis things like \*FOO, *$k ... nothing seems to work.
UNLESS I take out strict, then it works.

Anyone know what's happening here.

Thanks,
Bill
Jul 19 '05 #1
2 7112
Bill wrote:
I'm trying to use a hash key as a filehandle like so.

#!/usr/local/bin/perl
use strict;

my %buf = (
'F00' => 'foo.dat'
);

&open_files();

exit 0;

sub open_files {
while(my($k,$v)=each(%buf)) {
open ($k, ">$v") || die $!;
}
}


If you're using a reasonably recent version of perl, you can use this:

#!/usr/bin/perl
use strict;
use warnings;

my %buf = (
'F' => 'foo.dat',
'B' => 'bar.dat',
);

{ # Bare block to delimit scope of %fh
my %fh = open_files(%buf);
print {$fh{B}} "One line to file 'B' ($buf{B})\n";
print {$fh{F}} "Another to file 'F' ($buf{F})\n";
} # All %fh file handles get closed here
exit 0;

sub open_files {
my %filenames = @_;
my %filehandles;
while(my($k,$v)=each(%filenames)) {
open $filehandles{$k}, '>', $v or die "Cannot open '$k' file $v: $!\n";
}
%filehandles;
}

-Joe
Jul 19 '05 #2
fu****@yahoo.com (Bill) wrote in message news:<39*************************@posting.google.c om>...
I'm trying to use a hash key as a filehandle like so.

#!/usr/local/bin/perl
use strict;

my %buf = (
'F00' => 'foo.dat'
);

&open_files();

exit 0;

sub open_files {
while(my($k,$v)=each(%buf)) {
open ($k, ">$v") || die $!;
}
}
But, I get the following error:
Can't use string ("F00") as a symbol ref while "strict refs" in use at
./try.pl line 14.

I've tried varouis things like \*FOO, *$k ... nothing seems to work.
UNLESS I take out strict, then it works.

Anyone know what's happening here.


Yes, you are trying to use a symbolic reference.

Either stop trying to use a symbolic reference or tell Perl that you
really want to.

You can't use \*FOO as a hash key - or rather if you try it'll be
converted into a string like 'GLOB(0x8105afc)' because hash keys are
strings.

There is such a thing as Tie::RefHash that would allow you to use a
reference as a hash key but I wouldn't recommend it.

My advice is take a step back and decide what it is you were trying to
achive by using filehandles as hash keys and come up with another way
to do it (probably involoving autovivified dynamic file handles).

This newsgroup does not exist (see FAQ). Please do not start threads
here.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jorge Godoy | last post by:
Hi! I'm trying to get a specific information from inside an image and it works correctly on unices. What I do is: 1. associate a filehandle with the image file 2. get the desired line 3....
1
by: Eduard W. Lohmann | last post by:
Hello. I write this little thing to help me log from several instances of the same class in apache, mod_perl. But I can't figure one thing out. package Logger; require Carp; # Write the...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
1
by: Wijaya Edward | last post by:
Hi all, I have the following code: import sys import re ham_count = 0 spam_count = 0
1
by: Zairay | last post by:
Hi All, I'm having a problem with the Shell32.dll when I try to open a program from my Access db. When I try to open a program called FalconViewLite from my access database I get an error in...
1
by: nehaz | last post by:
i wanted to open a file using perl and the output of print cmd to be added in that file. can i do it using perl ? what if i use following the following ".pl " file. open CONFIG, ">HELLO.txt";...
7
by: freshpetals | last post by:
Hello people..I just wanted to know that can we store number of files in a filehandle ..if yes how could we do that???I did write a program but its just parsing result from the file which I'm giving...
11
by: Sevla | last post by:
hello im novice perl programmer, and i need a little help about manipulating files, as text.txt for example, i need seek some specific "word" or information on the txt file or any external...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.