Professional Web Applications Themes

passing global variable by reference? - PERL Modules

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 ...

  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. #2

    Default Re: passing global variable by reference?

    Don S <dshesnicky> 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

  3. #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 :)
    doentation/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

Similar Threads

  1. #40737 [NEW]: Variable passing by reference instead of copying.
    By anter at voliacable dot com in forum PHP Bugs
    Replies: 2
    Last Post: March 6th, 11:34 AM
  2. Passing Variables by Reference
    By babo_ya in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: February 22nd, 10:29 PM
  3. passing Conf object from Global.asa to a dll
    By Aksahy in forum ASP Components
    Replies: 1
    Last Post: August 13th, 01:02 PM
  4. Replies: 0
    Last Post: July 17th, 09:23 AM
  5. Replies: 7
    Last Post: July 17th, 04:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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