been using Java too long: please help with dynamic "our" declaration

R

Robert Watkins

I've been away from Perl too long, folks, and I'm rusty.

Can't figure out how to declare a few dynamically generated names (i.e.
they could change with each run of the script) as "our" vars.

The idea is to use the names as filehandles, and to write to different
files based on what's found:

--------------------------------------------------------
our $name;
*name = *File::Find::name;

// this is the equivalent of what the script might see
// from the dynamic input:
my @statuses = ("New", "Updated", "Commented and Updated");

my $statuses = '(' . join("|", @statuses) . ')';
my $data;
my ($fh, $filename);

foreach $fh (@statuses) {
$us = $fh;
$us =~ s/\s+//g;
$filename = "blah.$us.list";
open($fh, ">$filename") or die "Could not create $filename: $!\n";
}

File::Find::find( {wanted => \&wanted, follow => 1}, CONTENT_DIR);

foreach $fh (@statuses) {
close($fh);
}

sub wanted
{
local $/;
my $data;
if (/^(sect0|withdrawal)-meta\.html\z/s) {
open(FH, "<$name") or warn "Can't open $name: $!\n";
$data = <FH>;
close(FH);
if ($data =~ /<meta name="unitStatus" content="($statuses)">/) {
$fh = $1;
print $fh "$name\n";
}
}
}
--------------------------------------------------------

Any suggestions?

Thanks,
-- Robert
 
U

usenet

Robert said:
if (/^(sect0|withdrawal)-meta\.html\z/s) {

Did you mean to say something like:
if (my ($name) = (/^(sect0|withdrawal)-meta\.html\z/s)) {

Because I can't see where you're getting a value for $name in the code
you posted.

BTW, are you on UNIX or Windows?
 
R

Robert Watkins

(e-mail address removed) wrote in @g49g2000cwa.googlegroups.com:
Because I can't see where you're getting a value for $name in the code
you posted.
our $name;
*name = *File::Find::name;
BTW, are you on UNIX or Windows?
UNIX

But what about getting the right values into $fh and using them as
filehandles?

-- RW
 
T

Tad McClellan

Robert Watkins said:
Obviously this is one to remember.


That's how it is with most Frequently Asked Questions.

perldoc -q filehandle

How can I make a filehandle local to a subroutine? How do I pass file-
handles between subroutines? How do I make an array of filehandles?
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top