Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using and renaming same variables/hash within different subroutines

by Sun751 (Beadle)
on Jun 24, 2009 at 08:15 UTC ( [id://774297]=perlquestion: print w/replies, xml ) Need Help??

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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://774297]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found