Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing Perl script for my project, where I am passing same variable to different sub routine with different name so that it won’t collide with same variable within another subroutine, Though, the variable holds same value due to continuous renaming in different subroutine its getting bit confusing and looks like bit of mess , for example,
my (%resource,$portoffset); initialize(\%resource,$portoffset); consolidate(\%resource, $portoffset); dispatch(\%resourcel,$portoffset); sub initialize { my ($HRI_resource,$I_portoffset) = @_; } sub consolidate { my ($HRC_resource, $C_portoffset)= @_; } sub dispatch { my ($HRD_resource, $D_portoffset)= @_; }
Any one have any suggestion regarding this? Please let me know. Cheers
  • Comment on Using and renaming same variables/hash within different subroutines
  • Download Code

Replies are listed 'Best First'.
Re: Using and renaming same variables/hash within different subroutines
by why_bird (Pilgrim) on Jun 24, 2009 at 08:26 UTC
    ...so that it won’t collide with same variable within another subroutine...
    There shouldn't be any collision, if you 'my' something within a subroutine, that variable should be scoped only to the end of that block (i.e. to the end of the subroutine). So you can call two variables in two different subroutines the same thing.

    Look here for a good explanation of 'lexical scoping' (i.e. declaring variables with 'my')

    why_bird
    edit: added link
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
Re: Using and renaming same variables/hash within different subroutines
by Utilitarian (Vicar) on Jun 24, 2009 at 08:30 UTC
    Because you are using the my keyword you do not need to worry about this, ie you do not need to distinguish the variables used.
    sub initialize { my ($resource_ref,$portoffset) = @_; }
    Where you pass references to the resource hash they are all pointing to the same thing and where you pass the value of $portoffset there can be no collision as my lexically scopes the variable to within the block.

    If you need the subroutines not to interfere with %resources, you'll have to copy the values into a local hash for manipulation in the subroutines

Re: Using and renaming same variables/hash within different subroutines
by Anonymous Monk on Jun 24, 2009 at 08:25 UTC
    There is no collision
    #!/usr/bin/perl -- use strict; use warnings; use diagnostics; my ( %resource, $portoffset ); $portoffset = 6; warn $portoffset; initialize( \%resource, $portoffset + 1 ); consolidate( \%resource, $portoffset + 2 ); dispatch( \%resource, $portoffset + 3 ); warn $portoffset; sub initialize { my ( $HRI_resource, $portoffset ) = @_; warn $portoffset; } sub consolidate { my ( $HRC_resource, $portoffset ) = @_; warn $portoffset; } sub dispatch { my ( $HRD_resource, $portoffset ) = @_; warn $portoffset; } __END__ 6 at temp.pl line 8. 7 at temp.pl line 17. 8 at temp.pl line 22. 9 at temp.pl line 27. 6 at temp.pl line 13.
    perlintro, Variables and Scoping, Use strict warnings and diagnostics or die.
Re: Using and renaming same variables/hash within different subroutines
by AnomalousMonk (Archbishop) on Jun 24, 2009 at 13:58 UTC