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

Discussion in 'Perl Misc' started by Robert Watkins, Jan 25, 2006.

  1. 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
    Robert Watkins, Jan 25, 2006
    #1
    1. Advertising

  2. Robert Watkins

    Guest

    Robert Watkins wrote:
    > 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?

    --
    http://DavidFilmer.com
    , Jan 25, 2006
    #2
    1. Advertising

  3. wrote in news:1138220443.432428.58930
    @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
    Robert Watkins, Jan 25, 2006
    #3
  4. Jim Gibson <> wrote in news:250120061244141829%
    :

    > Yes. Don't use dynamically generated variables. Use a hash of
    > filehandles instead:
    >

    Brilliant. Thank you. Obviously this is one to remember.
    -- RW
    Robert Watkins, Jan 25, 2006
    #4
  5. Robert Watkins <> wrote:
    > Jim Gibson <> wrote in news:250120061244141829%
    > :
    >
    >> Yes. Don't use dynamically generated variables. Use a hash of
    >> filehandles instead:
    >>

    > 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?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amanda
    Replies:
    26
    Views:
    954
    amanda
    Nov 11, 2006
  2. Replies:
    0
    Views:
    583
  3. JVRudnick

    CDOSYS executes but takes TOO TOO long?

    JVRudnick, Feb 20, 2008, in forum: ASP General
    Replies:
    13
    Views:
    285
    JVRudnick
    Feb 28, 2008
  4. Iain Barnett
    Replies:
    10
    Views:
    222
    Brian Candler
    Aug 18, 2010
  5. JerryP
    Replies:
    2
    Views:
    106
    John W. Krahn
    Oct 28, 2007
Loading...

Share This Page