Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to pull the value of $epname into another part of my script, a second if statement. However when I try to use in the second if statement I get an error saying I have not declared the $epname variable. I have the print statement between the two if statements as a check. It prints the correct value (it is printed several times but i think this is because its in another loop). What am I doing wrong here.

# Get name if($tmpName eq 'Episode_Name'){ $ +epname = $tmpValu;} print "$epname - Title"; # Change Title if($tmpName eq 'Title_Brief'){ $newTitle = $epname; $xml->{Asset}->[0]->{Metadata}->[0]->{App_Data}->[$appkey]->{Value} = +$newTitle; }

Replies are listed 'Best First'.
Re: Using a value to add to xml line
by AnomalousMonk (Archbishop) on Nov 10, 2016 at 20:00 UTC

    As hippo has pointed out, you haven't really shown us any code. I'm guessing, however, that you have declared and are using  $epname as a lexical variable within your "loop," and so the variable name vanishes when the loop is exited:

    while (some_condition_is_true()) { ... my $epname = some_string(); ... print "for debug: \$epname is '$epname' \n"; ... } $newTitle = $epname; # variable does not exist here
    If you want a lexical  $epname to exist and have the value it was given in the loop when it's outside the loop, do something like:
    my $epname; while (some_condition_is_true()) { ... $epname = some_string(); # do NOT declare new variable ... } $newTitle = $epname; # variable exists, has value here


    Give a man a fish:  <%-{-{-{-<

Re: Using a value to add to xml line
by hippo (Archbishop) on Nov 10, 2016 at 17:00 UTC

    Have you declared $epname anywhere? You have not shown that in the provided extract.