Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Aliasing and return, how does return work?

by bsb (Priest)
on Jun 30, 2004 at 03:18 UTC ( [id://370674]=perlquestion: print w/replies, xml ) Need Help??

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

On Preserve @_ aliasing a (scalar||array) I wrap a scalar parameter into an array while preserving it's aliasing. A problem I've just encountered it that it doesn't preserve the aliasing on the $list container itself, and I think it's because "return $x" returns a copy of whatever and I can't think of an aliasing equivalent...

If that made no sense it doesn't really matter. I want a sub "ret" which will print "ALIAS" below

DB<28> sub ret { return $_[0] } DB<29> $a = []; print \$_,"\n",\$a and \$_==\$a and print "ALIAS" fo +r ret($a); SCALAR(0x8201604) SCALAR(0x8201b08)
What I want can probably be achieved in two steps:
&wrap_arg_one; for my $list ($_[0]) { ...}
but I'd prefer something like &shift_list in the post above

Brad

Replies are listed 'Best First'.
Re: Aliasing and return, how does return work?
by japhy (Canon) on Jun 30, 2004 at 03:31 UTC
    Make it an lvalue function:
    sub ret :lvalue { $_[0] }
    Notice: I didn't put return() in there -- that's a necessity for an lvalue function.
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      I think this could work but I'm not having much joy so far. I'm still on perl version 5.6.1 which could easily be a factor.

      Will an lvalue sub work when called as &ret = 123?

      I ask because I'm trying to do parameter preprocessing for the caller and was doing it with &ret.

        I'm not sure if lvaluable subs work in 5.6.1. This code works for me with Perl 5.8.4:
        sub ret :lvalue { $_[0] } sub foo { &ret++; print "ARGS: <@_>\n"; } my ($x, $y) = (10, 20); foo($x, $y); __END__ ARGS: <11 20>
        Make sure you aren't accidentally shifting the argument you're planning on setting before you set it.
        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found