in reply to variable has value but then become empty
First, please wrap your code, data and expected output within <code></code> tags.
You should always use strict; and use warnings;. I believe the problem you're having is that you don't define $public_key_type prior to the loop. perl lexicalizes variables, so if you define something within a scope, that variable will not be accessible outside of that scope; this is by design.
You don't show the beginning of the while loop, so I've guessed. Try something like the following (untested):
use strict; use warnings; my $public_key_type; while (my $line = <DATA>){ if ( $line =~ /^Public Key Type:/ ) { $public_key_type = scalar <DATA> for 1; $public_key_type =~ s/^\s+//; $public_key_type =~ s/\s+$//; $public_key_type =~ s/\R//g; } } print "$public_key_type\n";
-stevieb
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: variable has value but then become empty
by AnomalousMonk (Archbishop) on Sep 09, 2015 at 16:19 UTC |