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
In reply to Re: variable has value but then become empty
by stevieb
in thread variable has value but then become empty
by Randy_j53
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |