473,471 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File handle - For a list of files

1 New Member
Hi,

I am new in Perl and need abit of help on the usage of file handler.
I was trying to write a line into a file with file handler ID "oFile2_$macro", but it does NOT work with the following Error :
String found where operator expected at /home/wongz/bin/pt/grab_clock_group.new.pl line 112, near "$macro "$_""
(Missing operator before "$_"?

############ START SNIPPET ###########
my $group = shift(@ARGV);

# Create files with filename from the argument array
foreach $macro (@ARGV) {
my $outfile ="$inFile"."_${group}_$macro";
print "Output file: $outfile \n";
open("oFile2_$macro", ">$outfile") or die "Can't open `$outfile': $!";
}

## Write a line into file with file handler name "oFile2_$macro"
## Error occured on the following line
## I tried by double-quoting the file handler like\\\\\\print "oFile2_$macro" "$_" ;\\\\\\\\ but it still does NOT work.

print oFile2_$macro "$_" ;

############ END SNIPPET ###########

Does anyone know the correct way to set the file handler name ?

Thanks
Aug 14 '07 #1
2 1909
miller
1,089 Recognized Expert Top Contributor
You need to study more about proper names for file handles. Or better yet, start using lexicals instead.

The following uses a hash to store your open file handles

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $group = shift @ARGV;
  4.  
  5. my %outfh;
  6.  
  7. # Create files with filename from the argument array
  8. foreach my $macro (@ARGV) {
  9.     my $outfile = join '_', $inFile, $group, $macro;
  10.     print "Output file: $outfile \n";
  11.     open my $fh, '>', $outfile or die "Can't open $outfile: $!";
  12.     $outfh{$macro} = $fh;
  13. }
  14.  
  15. while (my ($march, $fh) = each %outfh) {
  16.     print $fh "Hello World\n";
  17. }
  18.  
- Miller
Aug 14 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
In addition to Miller's comments, here is a bit of light reading about the open() function.

Regards,

Jeff
Aug 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Kim Jensen | last post by:
I'd like to make a directory listing where instead of the entire filename I need it to show the filename minus the extention and get the value of charname= in the file itself. I've been told...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
0
by: eholz1 | last post by:
Hello PHP group, I have a question about file locations, and whether on not (in my case, not) a page will execute depending on the location of the php file. I have a web devel server, with a...
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
2
by: nothing1 | last post by:
Here is my code and what it does is create a page for every directory with the contents of choice. Its been working on all letters except "u" I can't seem to figure it out. Does someone see the...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.