passing global variable by reference?

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: passing global variable by reference?

    Don S <dshesnicky@yahoo.com> wrote:
    > 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.
    My newsreader wrapped your code and I couldn't read it very well, but..

    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

  4. #3

    Default Re: passing global variable by reference?

    > 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--;
    >
    That pretty much seemed to work, should have tried
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139