How to read the password from the text file in perl
You can store password in file with limited permissions, but using ssh keys is still better solution.
my $sftp = Net::SFTP::Foreign->new(
'auser@sftp.rent.com',
password => get_passw("chmod_600_passw_file"),
more => ['-v']
);
sub get_passw {
my ($file) = @_;
open my $fh, "<", $file or die $!;
my $pass = <$fh>; # do { local $/; <$fh> };
chomp($pass);
return $pass;
}
If you want to store both user/pass in file separated with :
you can,
my $sftp = Net::SFTP::Foreign->new(
get_credentials("chmod_600_passw_file"),
more => ['-v']
);
sub get_credentials {
my ($file) = @_;
open my $fh, "<", $file or die $!;
my $line = <$fh>;
chomp($line);
my ($user, $pass) = split /:/, $line;
return ($user, password => $pass);
}
Read password using perl on windows
To read password you can also use Term::ReadLine.
For further details visit Perl Monk.
Reading in key-values from a text file in Perl, skipping comments
You have some solutions, but I thought it would be interesting to explain some of the problems with your existing code.
You say:
Also, the if conditional doesn't work. I still see comments and the description printed.
Let's start by looking at that.
Your conditional is this:
if (my @entrykv = split /:\s*/) {
...
} else {
...
}
You're hoping for that to distinguish between your key/value lines and the lines that contain the description (and, therefore, don't contain a ":"). I think you misunderstand what split()
does. In the case where the line contains a ":", split()
returns a list with two items. That list gets put into the array, @entrykv
, and the if
statement evaluates that array in a scalar context which gives the value 2 (as there are two elements in the array). 2 is a true value, so we get into the "true" part of the if
/else
statement.
In the case where we don't have a ":" in the line, split()
will return a list with a single element (containing the whole line). That, again, gets put into @entrykv
and the if
statement evaluates that in scalar context. That gives the value 1 and 1 is still a true value and we still end up in the "true" part pf the if
/else
statement.
So you need to do something different here. The best approach is probably to check for the existence of a ":" in the line. Something like this:
if (/:/) {
my @entrykv = split /:\s*/; #/ stupid SO highlighter
print "@entrykv\n";
} elsif (! /^#/) {
print "Not a comment\n";
$desc{$key} .= $_;
}
Oh, and that's a second point. You used +=
to add to the end of the description. That's not right. Perl uses .
as the concatenation operator - so you need .=
here.
And there's one other problem here that goes right to the heart of the design. You have separate hashes for each of the attributes. That's a really bad idea. IF you have a data item with many attributes, you should try to keep all of those attributes together in a single variable. And a hash is a perfect way to store all of those attributes together.
Your read_entry()
subroutine reads a single file. And I think that each file just contains a single "key" (e.g. "sample"). So I think it makes sense for that subroutine to return a reference to a hash which contains all of the data from that file. It might look like this:
sub read_entry {
my ($filename) = @_;
my $key = $filename;
$key =~ s/\.txt$//; #/ stupid SO highlighter
# use a) lexical filehandle and b) three-arg version of open()
open my $fh, '<', $filename or die "Can't open $filename: $!\n";
my %data;
while (<$fh>) {
# Skip comments
next if /^#/;
if (/:/) {
chomp;
my ($key, $val) = split /:\s*/; #/ stupid SO highlighter
$data{$key} = $val;
} else {
$data{desc} .= $_;
}
}
return \%data;
}
Using CAM::PDF For Perl - Can I Read Text From A (v1.7) Password-Protected PDF File?
I'm the author of CAM::PDF. It's probably an newer PDF feature that CAM::PDF doesn't support. I wrote the encryption support back in PDF 1.2 days and have barely updated it since. So most likely it's not your fault but is a limitation of the library.
Perl: read data file as map
you could use Tie::File::AsHash.
use Tie::File::AsHash;
tie my %map, Tie::File::AsHash::, $path_to_file, split => qr/\s*=\s*/, join => '='
or die "failed to open: $!";
$map{Password} = 'swordfish'; # this actually changes the file!
print 'The password is ', $map{Password}, "\n";
How do I read the contents of a small text file into a scalar in Perl?
From the Perl Cookbook:
my $filename = 'file.txt';
open( FILE, '<', $filename ) or die 'Could not open file: ' . $!;
undef $/;
my $whole_file = <FILE>;
I would localize the changes though:
my $whole_file = '';
{
local $/;
$whole_file = <FILE>;
}
Related Topics
Aosp Build Error: Unrecognized Module Type "Hidl_Package_Root"
Git Is Not Ignoring File Permission Changes
/Usr/Bin/Ld: Cannot Find -Lemu
Run Random Command in Bash Script
Grep If The Next X Lines Doesn't Contain a Specific String
Command Execution with Nohup in Background
Bash - Ignore Hidden Files and Empty Source Directory When Copying
Git Wont Reset File Permissions Over Cifs Mount
Shell Script to Find The Nth Occurrence of a String and Print The Line Number
Find Command Search Only Non Hidden Directories
How to Pass Local Shell Script Variable to Expect
Gitlab: You Are Not Allowed to Access Master! After Moving Gitlab to Another Server
How to Find Libstdc++.So.6: That Contain Glibcxx_3.4.19 for Rhel 6
Change The Escape Sequence Generated by Xterm for Key Combinations