Perl reading a file
hi all,
Why if i did read a file I don't have something in $a and $b? Code:
open(FL, "file"); |
Quote:
Code:
use strict; Did you check success/failure of your 'open' operation ? (No, you didn't.) |
Well, I don't know. When I ran this:
Code:
#!/usr/bin/perl Code:
syntax error at ./3.pl line 5, near "{ " |
Quote:
Code:
cat -n file_to_examine |
Quote:
Code:
open my $file_handle, '<', "file" Next, the line $a = <FL> provides a scalar context for <>, so you are only going to get the first line of the file put into $a. Therefore, the next bit (which locally changes the record separator to undefined) is not really going to have any effect, as far as I can see. I don't know what you really want to do here, but if you want to get an entire file into a scalar, you can look into this article or the File::Slurp module on the CPAN. If you want to slurp in a file, keep in mind that it should be reasonably small. A huge file will gobble up a chunk of memory if you try to slurp it. In any case, here's one way to do it without the File::Slurp module: Code:
#!/usr/bin/env perl |
Do add
Code:
use strict; In any case, you have no ';' at the end of that line Code:
$a = <FL> Do check your open/close cmds for errors eg Code:
open(FILE, "<", "filename") or die " Unable to open filename: $!\n"; |
Quote:
|
Quote:
Code:
my $file = do { local $/; <$file_handle> }; |
Quote:
|
Wait,I think what I've a entangle about there '$/'. What's a chapter into perldoc talking about that?
|
Quote:
Can you please try to explain more clearly what you need to do? I understand that you want to copy an entire file into a scalar (for you, $a), but why do you want to copy that into a second scalar (for you, $b)? What are you actually trying to do here? What is the larger goal? |
I can read file there
my $file = do { local $/; <$file_handle> }; but if I tried doing it there my $file = <$file_handle>; It doesn't work. I want to find out mistake into my code, I ack about that before... http://www.linuxquestions.org/questi...-parse-701192/ I think what if I would doing read file without local $/;, it will do work. hm... Quote:
|
Quote:
Code:
my $file = <$file_handle> |
Quote:
Code:
#!/usr/bin/env perl |
Quote:
|
All times are GMT -5. The time now is 06:36 PM. |