Dear Monks,
I have this snippet (credit
Aighearach) that try to open files under subdirectory 'mysubdir'
and then stored them into a hash with filename as key and its content as values.
#!/usr/bin/perl -w
use Data::Dumper;
use strict;
my $mydir = 'mysubdir\*';
my @filear = glob($mydir);
my %hash = make_hash(@filear);
sub make_hash {
(local $/)=undef;
my (@files,%hash);
@files = @_;
foreach my $file ( @files ) {
open( my $fh, $file ) or next;
$hash{$file} = <$file>; # This line gives "unopened filehandle wa
+rning"
};
return %hash;
}
print Dumper \%hash;
However, instead of giving the file contents as its values,
it gives "undef" instead, like this.
$VAR1 = {
'mysubdir\\file1.txt' => undef,
'mysubdir\\file2.txt' => undef
};
Please advice what's wrong with my code above.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.