Trivial perl doubt about FILE


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trivial perl doubt about FILE
# 1  
Old 07-28-2011
Trivial perl doubt about FILE

Hi,

In the following perl code:

Code:
#!/usr/bin/perl -w

if (open(FILE, "< in_file")) {
    while (<FILE>) {
        chomp($_);
        if ($_ =~ /patt$/) {
            my $f = (split(" ", $_))[0];
            print "$f\n";
        }
    }
    close FILE;
}

Why changing the "FILE" as "$FILE" gives error in the while loop and in the close statement?
# 2  
Old 07-28-2011
Which error, and did you change it everywhere?
# 3  
Old 07-28-2011
Quote:
Originally Posted by royalibrahim
Hi,

In the following perl code:

Code:
#!/usr/bin/perl -w
 
if (open(FILE, "< in_file")) {
    while (<FILE>) {
        chomp($_);
        if ($_ =~ /patt$/) {
            my $f = (split(" ", $_))[0];
            print "$f\n";
        }
    }
    close FILE;
}

Why changing the "FILE" as "$FILE" gives error in the while loop and in the close statement?

FILE is the name of the file handle,
in_file is the name of the file you are trying to read.
# 4  
Old 07-28-2011
I think you may be mistaking Perl for Bash, the variable $FILE is always $FILE and is a different thing to the file handle FILE, best practice these days suggests that you should open files with lexically scoped file handles (and the 3 argument form of open) so you should never see a bareword like FILE in your source.

Code:
if ( open( my $file, '<' , 'in_file' ) ){
   while(<$file>){
...

This User Gave Thanks to Skrynesaver For This Post:
# 5  
Old 07-28-2011
In the context in which you are using it FILE is what is known in Perl as a bareword.

A bareword is any combination of letters, numbers, and underscores, and is not qualified by any symbols. That is, while $dog is a scalar and @dog is an array, dog is a bareword.

There are a couple areas in Perl where a bareword is allowed, this being one of them.
This User Gave Thanks to fpmurphy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

2. Shell Programming and Scripting

perl doubt

Hi, Please help me to understand the following code: perl -lne 'print if "$_ " =~ /(5 (?:\d+ ){5})\1/' What the regular expression "?:" does? Also, whether the expression "\1" is the same as in sed (i.e) printing the elements inside pair of parentheses? (3 Replies)
Discussion started by: royalibrahim
3 Replies

3. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Shell Programming and Scripting

Doubt in this trivial awk code

Hi, What is the difference in the following two awk one-liners? awk -F, '{s++} END {if (s == 1 && $4 > "09:10:00") {print $2, $4}}' f1 awk -F, '{s++} s == 1 && $4 > "09:10:00" {print $2, $4}' f1 Even though, all the 2nd column values have duplicate records, the first code does not give any... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. Shell Programming and Scripting

Doubt in Perl Variable declaration

Anyone please say what is the difference between $var and ${var} in perl Sometimes $var used and sometimes ${var} used in same program. Thanks in Advance, Prabhu ---------- Post updated at 09:34 AM ---------- Previous update was at 05:59 AM ---------- Any one please clarify (1 Reply)
Discussion started by: prsampath
1 Replies

6. Shell Programming and Scripting

Perl map doubt

Hello , Please can someone tell me what exactly happens when the below filehandler is chomped into an array and later mapped. $lcpLog="logcopy\@".getTimestamp."\log"; open CFg ,"< $lcpcfg"; chomp(@cfg = <CFG>); close CFG; @cfg=grep { $_ ne ' ' } map { lc + (split /\s*\/\//) }... (0 Replies)
Discussion started by: rmv
0 Replies

7. Programming

A trivial XOR doubt in a program

Hi, I am trying to reverse a string using the following program utilizing the Exclusive OR bit operation: int main() { char str = "Quraish"; char *p = str, temp; char *q = str + strlen(str) - 1; while ( p != q ) { if (*p != *q) { *p ^= *q; *q ^= *p; *p ^= *q;... (1 Reply)
Discussion started by: royalibrahim
1 Replies

8. Shell Programming and Scripting

Perl script doubt?

Dear friends, I have two files. In first file first column ($1), i have numbers. The second file containes, the same number in the fourth column($4). I need a output file, matching the first file column 1 with second file column 4. The main thing is the output file lines will be sorted... (4 Replies)
Discussion started by: vasanth.vadalur
4 Replies

9. UNIX for Dummies Questions & Answers

File Transfer that is not so trivial I guess

I have three computers A, B and C. To login to B and C I should use A because it has a SSH key. I don't have any other way of accessing these two computers. Now, if I need to transfer a file between B and C, I am unable to find a way that would work... because I don't know how to authenticate... (1 Reply)
Discussion started by: Legend986
1 Replies

10. Shell Programming and Scripting

perl doubt plz explain....

hi all i wrote a shell script which uses perl script my code is : >cat filename | while read i >do >perl -e 'require "/home/scripts/abc.pl" ; abc("$i")' >done perl script used will simply check syntax of Cobol programs but it didn't work for me so i asked my colleague he suggested... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question