passing global variable by reference?

From: Don S (dshesnicky_at_yahoo.com)
Date: 02/20/04


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



Relevant Pages

  • Re: variables problem
    ... > 1) using strict define a global hash variable, ... In a subroutine is where you most often use lexical ... I tend to keep a print out in the open, using global variables. ... Some, though, practice overkill with lexical variables. ...
    (comp.lang.perl.misc)
  • Subroutine question
    ... Lets say I have a section of code which is run multiple ... times in a program in at least two separate parts of the main program. ... I would like to put it into a subroutine, but the problem is that doing ... subroutines so that they use all of the global variables in the ...
    (comp.lang.fortran)
  • Re: [SPARK] Code safety and information hiding
    ... I'll accept a small number of well documented global variables sometimes if the alternative is worse. ... Each subroutine has some local variables and each package has some state and together they all cooperate according to what the whole is supposed to do. ... Then I decide to break the system into separate processes, and for the sake of discussion what was a subroutine originally becomes a separate process, all communicating using some form of IPC and still cooperating to achieve the supposed goals. ...
    (comp.lang.ada)
  • Re: return and undef
    ... This subroutine also calls the form_parse ... > do if you want to undef that private variable after it is returned? ... Global variables, like %FORM, are just that - ... That would force you to declare your ...
    (comp.lang.perl.misc)
  • Re: Subroutine question
    ... Random Programmer wrote in message... ... Lets say I have a section of code which is run multiple ... >pasting the code twice instead of passing it on to a subroutine. ... >subroutines so that they use all of the global variables in the ...
    (comp.lang.fortran)