Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Simple Pass By Reference

by tomazos (Deacon)
on Jul 26, 2005 at 00:00 UTC ( [id://478024]=note: print w/replies, xml ) Need Help??


in reply to Re: Simple Pass By Reference
in thread Simple Pass By Reference

That duplicates the content of $_[0] doesn't it? (Your first example)

I want to pass it by reference, and give it a meaningful identifier in the context of the subroutine. (ie not have to use $_[0] throughout the subroutine.) (Your second example)

-Andrew.


Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com

Replies are listed 'Best First'.
Re^3: Simple Pass By Reference
by friedo (Prior) on Jul 26, 2005 at 00:12 UTC
    You can use a simple scalar reference, then.

    sub alter_var { my $scalar_ref = shift; $$scalar_ref =~ s/a/b/; } my $scalar = "foobarbaz"; alter_var( \$scalar );

    For more, see perlreftut.

    Also note that esskar's second example, which works on $_[0] directly, will alter the scalar passed into the parameter list. (In this case, $_[0] is an alias, not a reference, so there is no need to dereference anything.)

      I kind of wanted to use a local alias for $_[0], rather than have to use a reference that I have to remember to dereference everywhere.

      I guess I just want to say:

      alias $var_in_sub = $_[0];

      Just to give $_[0] a meaningful name in the context of the subrountine, such that $var_in_sub and $_[0] are the same thing. In the same way as $var_elsewhere and $_[0] are the same thing when calling alter_var($var_elsewhere).

      I guess this can't be done in Perl, without using references and dereferencing - which is fine. No big deal.

      -Andrew.


      Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
        Wouldn't this be considered to be semi-alias? Well you create one more variables but still does not have the "contents" as it is still a pointer/reference. So memory is OK here?

        sub alter_var { my $aliasvar = $_[0]; # Nice to have C like proto types but... ${$aliasvar} =~ s/a/b/g; # ${$_[0]} =~ s/a/b/g; # This uses $_[0] and works }
        I guess this can't be done in Perl, without using references and dereferencing - which is fine. No big deal.
        Someone always has to dereference. I'm not sure, but I think you're after one of the alias modules (Lexical::Alias, Tie::Alias, Variable::Alias, Data::Alias).

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found