Re: File handle as a input variable
by Marshall (Canon) on May 06, 2009 at 00:43 UTC
|
open (my $File_Handle, "<", "some..path") or die "error msg";
Instead of \$Sample.txt , you need a path to a file. E.g. in
the above, probably what you mean? is:
open (my $File_Handle, "<", "Sample.txt") or die (...blah ...);
| [reply] [d/l] |
|
|
I tried that. But it does not work with use strict
I get an error :
Can't use string ("File_Handle") as a symbol ref while "strict refs" in use at Log_Preocessing.pl line 28.
use strict;
my $Sample = "C:\\Perl\\Ref.txt";
my $File_Handle = "File_Handle";
open $File_Handle, ">$Sample" or die "Cannot open $Sample.txt for read :$!";
print $File_Handle "hi";
while (<$File_Handle>)
{
}
| [reply] |
|
|
Do it exactly the way Marshall shows and it will work.
The reason you are getting that error is because $File_Handle is not an "undefined scalar variable"". So instead of setting it beforehand to "File_Handle", declare it and set it in the call to open: open my $File_Handle.
See open for more information on this.
| [reply] [d/l] [select] |
|
|
This: my $File_Handle = "File_Handle"; is wrong. delete that.
for the open, put a valid path in there, like:
open my $File_Handle, ">", "full_textual_path_to_a_file" or die ....
Later use: print $File_Handle "some stuff\n";
| [reply] |
|
|
|
|
Re: File handle as a input variable
by JavaFan (Canon) on May 06, 2009 at 00:56 UTC
|
It seems to me the input to your function is a string, not a filehandle.
Now, with some symbolic trickery, you can create a bareword filehandle with the given name, but it seems utterly weird to write a function that gets passed the name of a variable it should use.
So, I suggest you further elaborate on the purpose of your input variable. What is $File_Handle = "File_Handle"; supposed to mean? | [reply] [d/l] |
Re: File handle as a input variable
by jwkrahn (Abbot) on May 06, 2009 at 00:57 UTC
|
| [reply] |
Re: File handle as a input variable
by GrandFather (Saint) on May 06, 2009 at 21:22 UTC
|
Use appropriate markup (see Writeup Formatting Tips) and preview your node to check that the result is as you expect it.
Show us the actual code (copy and paste is your friend) that generated the error and, if it's in a sub, show us the calling code too.
If you want us to make the effort to help you, you need to make the effort to describe your problem clearly and accurately.
The key issue (as already implied by Marshall and discussed by JavaFan) is that you should probably be using my in your open to create a lexical file handle. If there is a bigger issue, you haven't told us about it!
True laziness is hard work
| [reply] |