LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 11-28-2005, 03:17 AM   #1
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 30
Perl file handler in recursive function


Hi all,

The short question:

How can I make a file handler local to a function? Something like

Code:
sub myfunction() {
  my FILE = open "thefile";
}
Which, btw, does not work.

The long version:

I'm writing a recursive function that processes some files. I've something like

Code:
file1.txt:
bla
bla
insert file2.txt
blabla
insert file3.txt

file2.txt
bla
insert file3.txt
The idea is that, if file2.txt is inserted, then it has to be processed, so the function skel looks like:

Code:
sub process_file() {
  my $file = shift;
  open FILE, $file;
  # some things are done
  if(there is reference to a file file.txt) {
    &process_file(file.txt);   
  }
  # finish processing
}
I cannot do things sequentially, because the results of an inner file have effects on the file containing it (indeed, what I'm trying to construct is the dependency tree of the files). The problem is that the file handler FILE is not local to the function, so it is "shared" by different instances of the function. I've tried to make something like "my FILE", but this does not work. How this has to be done in perl?

Thank you a lot in advance!!!
 
Old 11-28-2005, 03:31 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 47
You can use a variable as the filehandle:
Code:
open my $fh, $file;
# do stuff with $fh
close $fh;
 
Old 11-28-2005, 03:08 PM   #3
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Original Poster
Rep: Reputation: 30
Great. Thank you!!
 
Old 02-01-2009, 04:20 PM   #4
brian642
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
Thanks

this really solved a problem that I couldn't find the answer anywhere else.
 
Old 02-02-2009, 03:20 PM   #5
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
It's explained in the perlfaq, section 5, btw.

For more on recursive Perl and filehandles, check this book:

http://hop.perl.plover.com/book/

(Download available.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
howto know whether a function is called in signal handler snowing Programming 1 11-07-2005 09:55 PM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM
error recursive function in c shams Programming 3 07-28-2004 08:44 PM
gcc: recursive function quirk Disruptor Linux - Software 0 03-31-2004 11:58 AM
Recursive Function TriggerJ Programming 4 02-02-2004 08:58 PM


All times are GMT -5. The time now is 05:13 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration