This seems like something that should be simple, and I am most likely missing something.

I want to pass a my variable to a subrountine that then does something to it.

sub alter_var(???) { my $var_in_sub = ???; # alter $var_in_sub, eg $var_in_sub =~ s/a/b/g; } my $var_elsewhere = 'aaa'; alter_var($var_elsewhere); print $var_elsewhere; # should print 'bbb'

Any ideas as to what goes in the ??? bits? Essentially I want to make $var_in_sub an alias for $_[0].

Update: In the actual application the scalar is quite big, so I'd like to pass it by reference without copying the data.

I'd also like to use a meaningful name rather than $_[0] throughout the subroutine.

I'd also like to not have to reference (\$) and dereference ($$) all over the place.

I just want too much. :)

Lexical::Alias is what I want but I'm running 5.6.1, looks like 5.8.1+ is needed from the docs.

All bow to BrowserUK. Truly cool.

-Andrew.


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

In reply to Simple Pass By Reference by tomazos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.