Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: style q: duplication? of variables

by nakor (Novice)
on Aug 14, 2001 at 17:36 UTC ( [id://104760]=note: print w/replies, xml ) Need Help??


in reply to style q: duplication? of variables

Since arguments are passed _by alias_, doing things to $_[0] actually changes the first argument (and so on). E.g.:
sub frobnicate { $_[0] =~ s/foo/bar/; $_[0] =~ s/\d/\&/g; } my $var = "foo 12345"; frobnicate($var); # $var is now "bar &&&&&"
Of course, there is no "One True Way," but this is clean and works.

Replies are listed 'Best First'.
Re: Re: style q: duplication? of variables
by thpfft (Chaplain) on Aug 14, 2001 at 19:14 UTC

    Ouch. chastised in the chatterbox, i've digested all this and read a bit more thoroughly than before. I now understand that the creation of a new variable happens at this stage:

    my ($text) = @_;

    Which is embarrassingly obvious once realised. And that when I

    return $text;

    I am returning the new - local - entity and not the original one, so back in main::, the $text variable is now pointing to the value that was created in and returned from the sub. (Dominus' very lovely explanation of scope and duration was helpful here. That one really ought to be in best nodes.)

    And the reason things are different with references is that a duplicate of a reference points to the same piece of data as the original. There is still duplication, but only of pointers, not data.

    Thought i'd better put things straight before i get told off any more. Thanks for all the answers.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found