passing global variable by reference?
From: Don S (dshesnicky_at_yahoo.com)
Date: 02/20/04
- Next message: Michel Rodriguez: "Re: XML::DOM::Parser ignores ISO-8859-1 encoding?"
- Previous message: Regent: "Re[1]: Parsing MS Word documents"
- Next in thread: thumb_42_at_yahoo.com: "Re: passing global variable by reference?"
- Reply: thumb_42_at_yahoo.com: "Re: passing global variable by reference?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Feb 2004 07:29:37 -0800
I'm trying to stick my frequently used subroutines into a Sysadmin.pm
module. Of course the first one I tried uses global variables and I
wondering if someone can guide me though referencing and derefrencing.
It's to walk a tree and call a subroutine on files/links/directories.
(I think I actually got the original code from the camel book.)
Anyway,
I also need to use global variables such as the level of directories
being descended but I take it I also want to "use strict" as a good
programming practice.
I've read in the camel book and it notes the \$ reference and
$$dereference but doesn't outline usage in subroutines.
I'm trying to call it like this:
vvvvvvv
&tree_walk($top_dir,\$level,\&du_info,1,0,0);
the heart of the subroutine is (with errors):
my($path,$$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
&tree_walk($newpath,$level,$sub,$d_flag,$f_flag,$l_flag); # ... go
recursive
$level--;
and the actual subroutine is:
sub tree_walk{
my($path,$$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
my($newpath,$entry,@entries);
# Get all objects in the directory
opendir(DIR,"$path"); # open the
directory
my(@entries) = readdir(DIR); # get all entries
closedir(DIR); # close it
# Check all of the objects
foreach $entry (@entries) {
unless( $entry eq "." || $entry eq "..") { # filter always
$newpath="$path/$entry"; # create full
path
#-------- action on dirs, files, links ----------
if( -d "$newpath" && $d_flag == 1) { # DIRECTORIES
unless( -l "$newpath" ) { # ignore links
&$sub($newpath,'d');
}
}
#--------- recursively read directories ---------
unless( -l "$newpath" ) { # stay away
from links
if( -d $newpath ) { # if it is a
dir ...
&tree_walk($newpath,$level,$sub,$d_flag,$f_flag,$l_flag);
# ... go recursive
$level--;
}
}
}
}
}
Thanks,
Don
- Next message: Michel Rodriguez: "Re: XML::DOM::Parser ignores ISO-8859-1 encoding?"
- Previous message: Regent: "Re[1]: Parsing MS Word documents"
- Next in thread: thumb_42_at_yahoo.com: "Re: passing global variable by reference?"
- Reply: thumb_42_at_yahoo.com: "Re: passing global variable by reference?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|