I have been successfully reading files in a function for quite some time (see sample code snippet "WORKS" below). I am working on changing how I read the arguments that come into my function so that it is easier for other people to use. I tried changing to the use of a hash (see snippet "FAILS" below), but instead of getting the contents of the file that I opened, I get the glob address, e.g. GLOB(0x1499a8).
I feel like the answer is right in front of me, but I still cannot see it. Can anyone help?
WORKS
#!/usr/bin/perl
my $handle1;
open($handle1, "< /tmp/1") || die "Input file does not exist.\n";
# Case 1
GetFileContentsViaScalar(\*$handle1);
close($handle1);
sub GetFileContentsViaScalar
{
my $FH = shift;
print "SHIFT:\n";
{
local($/);
print <$FH>;
}
}
FAILS
#!/usr/bin/perl
my $handle1;
open($handle1, "< /tmp/1") || die "Input file does not exist.\n";
# Case 2 -- fails
GetFileContentsViaFnHash(\*$handle1);
close($handle1);
sub GetFileContentsViaFnHash
{
my %test;
$test{FH} = shift;
print "Function hash:\n";
{
local($/);
print <$test{FH}>;
}
}
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.