LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2009, 05:17 AM   #1
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Rep: Reputation: 15
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;
 
Old 02-04-2009, 06:00 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by umbrella2 View Post
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.)
 
Old 02-04-2009, 06:00 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
Old 02-04-2009, 06:04 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by wje_lq View Post
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.

Last edited by Sergei Steshenko; 02-04-2009 at 06:41 AM. Reason: Sorry, got confused - wje_lq did _not_ start the thread and the code is not his/her.
 
Old 02-04-2009, 06:49 AM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
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;
 
Old 02-04-2009, 06:52 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
Old 02-04-2009, 06:52 AM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by wje_lq View Post
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.
 
Old 02-04-2009, 07:24 AM   #8
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
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.
 
Old 02-04-2009, 07:35 AM   #9
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
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.
 
Old 02-04-2009, 08:09 AM   #10
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Wait,I think what I've a entangle about there '$/'. What's a chapter into perldoc talking about that?
 
Old 02-04-2009, 08:25 AM   #11
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
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?
 
Old 02-04-2009, 08:48 AM   #12
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
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.

Last edited by umbrella2; 02-04-2009 at 08:54 AM.
 
Old 02-04-2009, 08:54 AM   #13
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
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.
 
Old 02-04-2009, 09:00 AM   #14
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by umbrella2 View Post
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;

Last edited by Telemachos; 02-04-2009 at 09:38 AM.
 
Old 02-04-2009, 09:08 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by umbrella2 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl alarm not working when reading for a socket , perl seems to be buggy alix123 Programming 1 10-05-2008 07:36 PM
PERL pattern matching while reading a file content is dead slow gaynut Programming 13 08-13-2008 06:59 AM
perl : stop reading a file when condition ok frenchn00b Programming 7 07-05-2007 02:37 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration