Ask a Question related to PERL Modules, Design and Development.
-
Don S #1
passing global variable by reference?
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
Don S Guest
-
#40737 [NEW]: Variable passing by reference instead of copying.
From: anter at voliacable dot com Operating system: XP Prof SP2 PHP version: 5.2.1 PHP Bug Type: Class/Object related Bug... -
passing Conf object from Global.asa to a dll
hii all I am coding an application to make the portal send emails as needed. The email formation and sending is taken care of in the dll, as is... -
passing row types by reference
Hallo, I´m working with the new informix funcionality that allow use structure data like rows an list rows. I would like to know how I can... -
Help Please: 'Warning 5001 global variable "iF_timer" already defined in global scope
Hi there, I have just started to get this error: 'Warning 5001 global variable "iF_timer" already defined in global scope The variable name... -
passing javascript variable into asp variable using vbscript
The subject pretty much sums up what I need to do. Here is what I have so far, but still can't figure out how to get it working: <script... -
thumb_42@yahoo.com #2
Re: passing global variable by reference?
Don S <dshesnicky@yahoo.com> wrote:
My newsreader wrapped your code and I couldn't read it very well, but..> 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.
I believe this may be your problem:
my($path,$$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
If you change it to:
my($path,$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
and do a $$level--;
You may have better luck.
Personally, with recursive subs, or subs that need to work close together
I'll often use anonymous sub(s) from within a block:
sub do_something {
my(@arguments) = @_;
my($level);
..
.. Set variables that you'll need to keep between calls ..
..
my($worker) = sub {
# .. Perform work here
--$level; # Access $level, almost as though it were global.
};
return($worker->(@arguments)); # This actually runs it.
}
You get the general idea.
If you're using an OOP module, you can also put stuff like that in $self.
$self->{__level}; However, there is a performance penalty for using
hashes. Its minor, but if calling it several thousand times pr. second, it
can add up, Data::Dumper might show you a mysterious __level entry when
examining your object for other reasons, causing you to wonder where it
came from.
Jamie
thumb_42@yahoo.com Guest
-
Don S #3
Re: passing global variable by reference?
> I believe this may be your problem:
That pretty much seemed to work, should have tried>
> my($path,$$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
>
> If you change it to:
>
> my($path,$level,$sub,$d_flag,$f_flag,$l_flag) = @_;
>
> and do a $$level--;
>
a couple of more permutations to stumble on it :)
documentation/smockumentation. Relevant code is:
sub tree_walk {
my($level,$path,$sub,$d_flag,$f_flag,$l_flag) = @_;
<snip>
$$level++;
&tree_walk($level,$newpath,$sub,$d_flag,$f_flag,$l _flag);
$level--;
<snip>
}
I would have thought I needed a $$level or \$level when
I recursively called tree_walk but I guess it makes sense
that you have a pointer like object and once it's set you
only need to dereference it when used via $$.
Don
Don S Guest



Reply With Quote

