LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl reading a file (https://www.linuxquestions.org/questions/programming-9/perl-reading-a-file-702196/)

umbrella2 02-04-2009 05:17 AM

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");
$a = <FL>
{ local $/; $b = $a; }
close(FL);
print $a,$b;


Sergei Steshenko 02-04-2009 06:00 AM

Quote:

Originally Posted by umbrella2 (Post 3431661)
hi all,
Why if i did read a file I don't have something in $a and $b?
Code:

open(FL, "file");
$a = <FL>
{ local $/; $b = $a; }
close(FL);
print $a,$b;


Have you used

Code:

use strict;
use warning;

?

Did you check success/failure of your 'open' operation ? (No, you didn't.)

wje_lq 02-04-2009 06:00 AM

Well, I don't know. When I ran this:
Code:

#!/usr/bin/perl

open(FL, "file");
$a = <FL>
{ local $/; $b = $a; }
close(FL);
print $a,$b;

I got:
Code:

syntax error at ./3.pl line 5, near "{ "
syntax error at ./3.pl line 5, near "; }"
Execution of ./3.pl aborted due to compilation errors.


Sergei Steshenko 02-04-2009 06:04 AM

Quote:

Originally Posted by wje_lq (Post 3431698)
Well, I don't know. When I ran this:
Code:

#!/usr/bin/perl

open(FL, "file");
$a = <FL>
{ local $/; $b = $a; }
close(FL);
print $a,$b;

I got:
Code:

syntax error at ./3.pl line 5, near "{ "
syntax error at ./3.pl line 5, near "; }"
Execution of ./3.pl aborted due to compilation errors.


When you post code here, please do us a favor - copy-paste output of

Code:

cat -n file_to_examine
- we are really lazy and bug-prone to count lines manually.

Telemachos 02-04-2009 06:49 AM

Quote:

Originally Posted by umbrella2 (Post 3431661)
hi all,
Why if i did read a file I don't have something in $a and $b?
Code:

open(FL, "file");
$a = <FL>
{ local $/; $b = $a; }
close(FL);
print $a,$b;


A bunch of problems in the script. First, you really, really should stop using $a and $b as the name of your variables in Perl, since those two are used internally by the sort function. It's a terrible habit to get into. Second, the bareword filehandle (FL) in calls to open is not ideal and no longer necessary. Instead, you can now (since version 5.6 I think) use a scalar variable, and you should always check for errors when opening the file. It would look like this:
Code:

open my $file_handle, '<', "file"
  or die "Can't open 'file' for reading: $!"

See the excellent tutorial on open in perldoc, perldoc opentut or online here.

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
use strict;
use warnings;

open my $file_handle, '<', 'file-name'
  or die "Can't open 'file-name' for reading: $!";

my $file = do { local $/; <$file_handle> };

print $file;


chrism01 02-04-2009 06:52 AM

Do add
Code:

use strict;
use warnings;

to your code.
In any case, you have no ';' at the end of that line
Code:

$a = <FL>
and $a, $b are reserved vars in Perl used by the built-in sort cmd. Try different names. (you can use $a,$b but it causes confusion/problems)
Do check your open/close cmds for errors eg
Code:

open(FILE, "<", "filename") or die " Unable to open filename: $!\n";
http://perldoc.perl.org/

Telemachos 02-04-2009 06:52 AM

Quote:

Originally Posted by wje_lq (Post 3431698)
Well, I don't know. When I ran this:
Code:

#!/usr/bin/perl

open(FL, "file");
$a = <FL>              ## Semicolon missing HERE
{ local $/; $b = $a; }
close(FL);
print $a,$b;

I got:
Code:

syntax error at ./3.pl line 5, near "{ "
syntax error at ./3.pl line 5, near "; }"
Execution of ./3.pl aborted due to compilation errors.


You have no semicolon at the end of your fourth line. It should read $a = <FL>;. However, the script is a mess anyhow. See my post above this one.

umbrella2 02-04-2009 07:24 AM

Quote:

You have no semicolon at the end of your fourth line. It should read $a = <FL>;. However, the script is a mess anyhow. See my post above this one.
thanks for your reply. Currently I use it, but I need read a file and putted to $a variable, then I would to present a value per one line ... '$/'(cutting end lines).
Code:

my $file = do { local $/; <$file_handle> };
Because I need read overall from file to $a then putting value to $b and cut like this $/ only value into $b.

Telemachos 02-04-2009 07:35 AM

Quote:

Originally Posted by umbrella2 (Post 3431801)
thanks for your reply. Currently I use it, but I need read a file and putted to $a variable, then I would to present a value per one line ... '$/'(cutting end lines).
Code:

my $file = do { local $/; <$file_handle> };
Because I need read overall from file to $a then putting value to $b and cut like this $/ only value into $b.

No, you never need to put anything into $a or $b. See my post and Chrism's above for why. You can easily use what I gave you, however, to put the file into one variable and then copy it into another.

umbrella2 02-04-2009 08:09 AM

Wait,I think what I've a entangle about there '$/'. What's a chapter into perldoc talking about that?

Telemachos 02-04-2009 08:25 AM

Quote:

Originally Posted by umbrella2 (Post 3431834)
Wait,I think what I've a entangle about there '$/'. What's a chapter into perldoc talking about that?

Here: http://perldoc.perl.org/perlvar.html...CORD_SEPARATOR

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?

umbrella2 02-04-2009 08:48 AM

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:

Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)
So, I need two variables, first with just take value from file and second reformated to one line.

Telemachos 02-04-2009 08:54 AM

Quote:

Originally Posted by umbrella2 (Post 3431887)
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. I'm afraid why it doesn't work.

Nope, it won't work this way:
Code:

my $file = <$file_handle>
I explained why above, but I'll try again. That line provides a scalar context, and in a scalar context, the <> operator only reads in one line of input. The reason that it works with the funky do { local $/; <$file_handle> } block is that we've undefined the input record separator. Thus, in that case, one line = the whole file.

Telemachos 02-04-2009 09:00 AM

Quote:

Originally Posted by umbrella2 (Post 3431887)
So, I need two variables, first with just take value from file and second reformated to one line.

Ah, now we are getting somewhere. If you want to remove all the newline characters, you should be able to do that reasonably easily:
Code:

#!/usr/bin/env perl
use strict;
use warnings;

open my $file_handle, '<', 'redirect.php'
  or die "Can't open file for reading: $!";

my $file = do { local $/; <$file_handle> };

$file =~ s/\n//g; # Remove newlines repeatedly

# Do more stuff here;


Sergei Steshenko 02-04-2009 09:08 AM

Quote:

Originally Posted by umbrella2 (Post 3431887)
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...

So, I need two variable, first with just take value from file and second reformated to one line.

perldoc -f join


All times are GMT -5. The time now is 06:36 PM.