Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-28-2005, 03:17 AM   #1
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Perl file handler in recursive function

Hi all,

The short question:

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

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

insert file2.txt
insert file3.txt

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

sub process_file() {
  my $file = shift;
  open FILE, $file;
  # some things are done
  if(there is reference to a 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
Senior Member
Registered: Aug 2005
Posts: 1,755

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

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

Rep: Reputation: 0

this really solved a problem that I couldn't find the answer anywhere else.
Old 02-02-2009, 03:20 PM   #5
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:

(Download available.)


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: recursive function quirk Disruptor Linux - Software 4 09-23-2014 05:45 AM
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
Recursive Function TriggerJ Programming 4 02-02-2004 08:58 PM > Forums > Non-*NIX Forums > Programming

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

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