function parameter passing - copy or reference?

Quick and probably simple - but I'm c++

if function foo() takes scalar as parameters eg. sub foo { print "got: " . shift . "\n"; }
and I call it

$hashRef = { "0" => "blah blah" };

then calling foo makes the scalar string ${$hashRef}{'0'} copy or not?
Can I somehow verify it(in c++ i can using c-tor debugging)
LVL 43
Who is Participating?
If your function is
sub foo { print "got: " . shift . "\n"; }
then a copy will be made
but if  your function was something like
sub foo { $_[0]x=2 }
then you could verify that a reference to ${$hashRef}{'0'} was used
ravenplAuthor Commented:
> sub foo { print "got: " . shift . "\n"; }
OK, true, copy has to be made creating new string, but
sub foo { print "got: ",  shift,  "\n"; }

Also, what the 'x' means in
> sub foo { $_[0]x=2 }

Actually I'm wondering if
my $mail = $spamassassin->parse(${$data_ref}{'body'}, 0);
makes a copy during the call already?
I'm asking because SA allows passing message source as reference to array(where I know i'm passing reference only).
If $spamassassin->parse(${$data_ref}{'body'}, 0); makes a copy, it makes in the function, not in the call
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ravenplAuthor Commented:
ozo, are You familiar with SA internals? because sub foo { print "got: ",  shift,  "\n"; } will not make a copy - right?

Does switching to $spamassassin->parse(\@{$data_ref}{'body'}, 0); decreases the memory footprint?
ravenplAuthor Commented:
Of course SA has to keep local copy of the message somehow. My concern is copying because of the function call
In c++ functions either accepts reference(no copy) or value(copy just because of calling the function)
I think that sub foo { print "got: ",  shift,  "\n"; } will make a copy.

I wouldn't expect parse(\@{$data_ref}{'body'}, 0) to have much effect on the memory footprint,
but it really depends on how the function was written.

ravenplAuthor Commented:
ozo, from Your words I understand that calling foo($scalar) will not make a copy of the $scalar(will pass as kind of internal reference). The foo function body can make it copied if coded recklessly - right?
last Q then, why sub foo { $_[0]x=2 } will not make a copy and what the 'x' stands for.
perldoc perlop
       Binary "x" is the repetition operator.  In scalar context or if the
       left operand is not enclosed in parentheses, it returns a string
       consisting of the left operand repeated the number of times specified
       by the right operand.  In list context, if the left operand is enclosed
       in parentheses or is a list formed by "qw/STRING/", it repeats the
       list.  If the right operand is zero or negative, it returns an empty
       string or an empty list, depending on the context.

As you can observe by running the function, it modifies the original object.
ravenplAuthor Commented:
Looks like 'my $x = shift' performs the copy but accessing via $_[0] is not.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.