Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:40:27+00:00 2026-05-26T10:40:27+00:00

the perl code is like followed : the problem is that I can not

  • 0

the perl code is like followed : the problem is that I can not read $key inside sub tweak_server{} ….

my $key;
my %hash = ( flintstones => [ "C:/Users1/f1.xml", "C:/Users1/f2.xml" ], 
            jetsons => [ "C:/Users2/f1.xml" ], 
            simpsons =>    [ "C:/Users3/f1.xml", "C:/Users3/f1.xml", "C:/Users3/f1.xml" ], ); 

foreach $key (keys%hash){
if (scalar@{$hash{$key}}>1){
    foreach my $path (@{$hash{$key}}){
        my $filehandle;
        open($filehandle, "+<$path") or die "cannot open out file out_file:$!";
        my $roots = { TAG => 1 };
        my $handlers = { 'ROOT/TAG' => \&tweak_server,
                    };
        my $twig = new XML::Twig(TwigRoots => $roots,
                             TwigHandlers => $handlers,
                             twig_print_outside_roots => \*$filehandle);
        $twig->parsefile($path);
        say $key;#could read key
        sub tweak_server {
            my ($twig, $root) = @_;
            my $tag2=$root->first_child_text('TAG2'); 
            say $key;# could not read
            if ($tag2=~/$key/){
            #BLABLA
            }
            $twig->flush( $filehandle, pretty_print => 'indented');
        }
    }
}

}

as i state, the $key can be read outside the sub, but not inside..the error appeared:Use of uninitialized value $key

and then i tried a simple situation, just like

my $a="aaa";
open( $filehandle, "+<$path") or die "cannot open out file out_file:$!";
my $roots = { TAG => 1 };
my $handlers = { 'ROOT/TAG' => \&tweak_server,
            };
my $twig = new XML::Twig(TwigRoots => $roots,
                     TwigHandlers => $handlers,
                     twig_print_outside_roots => \*$filehandle
                     );
$twig->parsefile($path);
sub tweak_server {
    say $a;
    my ($twig, $root) = @_;
    my $tags=$root->first_child_text('TAG2');
    my $str="204B";
    if ($tag2=~m/$str/){
    foreach my $b(1...6){
                    say $a;             }
}
$twig->flush( $filehandle, pretty_print => 'indented');

}

in this code, the $a can be read….
i spent one day on this, but still can’t fix it…crazy now
thank in advance!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:40:28+00:00Added an answer on May 26, 2026 at 10:40 am

    You are declaring $key outside of the for loop. Then, inside the for loop, you define a subroutine that closes on $key.

    As a general rule, declare variables in the smallest applicable scope. For example:

    for my $key (keys ...) {
    

    or

    open my $filehandle, '<', ...
    

    Why are you defining sub tweak_server in the body of the for loop? It seems to me, what you want to do is to define a new anonymous sub for each iterationn.

    First, a short example that replicates what you are observing:

    use warnings; use strict;
    
    my $key;
    my %hash = qw(a b c d e f);
    
    foreach $key (keys %hash) {
        somesub();
        sub somesub {
            print "$key\n";
        }
    }
    

    Now, the fix:

    use warnings; use strict;
    
    my %hash = qw(a b c d e f);
    
    foreach my $key (keys %hash) {
        my $somesub = sub { print "$key\n" };
        $somesub->();
    }
    

    This way, we define a new anonymous function at each iteration and each new sub closes over each value of the loop variable.

    In terms of your code, you should then replace the named sub with

    my $tweak_server = sub {
        my ($twig, $root) = @_;
        my $tag2=$root->first_child_text('TAG2'); 
        say $key;# could not read
        if ($tag2=~/$key/){
            #BLABLA
        }
        $twig->flush( $filehandle, pretty_print => 'indented');
    }
    
    my $handlers = { 
        'ROOT/TAG' => $tweak_server,
    };
    

    Or, even better, as @mirod observes, pass $key to tweak_server:

    sub tweak_server { 
        my( $key, $twig, $root)= @_;
        ...
    }
    

    And, in the body of the loop,

    my $handlers = { 
        'ROOT/TAG' => sub { tweak_server($key, @_) },
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some Perl code like this: my $match = $matches{$key} ? $matches{$key} was
So I have some perl code that goes something like: use strict; use XML::XPath;
I have some perl code that looks something like this: my @array = map
I have some Perl code that executes a shell script for multiple parameters, to
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
Any suggestion how I can document my Perl code? What do you use and
I want to write a Perl code that ends the process of another program
I'm trying to decrypt a Perl code which I'm not familiar with, somehow related
So i'm writing a quick perl script that cleans up some HTML code and
I've been running across a lot of Perl code that breaks long strings up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.