where to put dclone

G

Graham Wood

I have a function that accepts an array reference as an argument and
returns 2 array references. I'm "dclone"ing the references so that when
I call the function again I'm not using the same references again. As in ...
=========================================================

($ref1,$ref2)=return_2_refs($input_ref1);
$ref3=dclone($ref1);
$ref4=dclone($ref2);

($ref5,$ref6)=return_2_refs($input_ref2);
=========================================================

Can anyone tell me how to incorporate the dclone into this line:
($ref1,$ref2)=return_2_refs($input_ref1);

So that I don't have to create the extra $ref3 and $ref4?

I tried ($ref1,$ref2)=dclone(return_2_refs($input_ref1));
but that gave me nothing in $ref1.

Thanks in advance

Graham
 
J

John W. Krahn

Graham said:
I have a function that accepts an array reference as an argument and
returns 2 array references. I'm "dclone"ing the references so that when
I call the function again I'm not using the same references again. As in
...
=========================================================

($ref1,$ref2)=return_2_refs($input_ref1);
$ref3=dclone($ref1);
$ref4=dclone($ref2);

($ref5,$ref6)=return_2_refs($input_ref2);
=========================================================

Can anyone tell me how to incorporate the dclone into this line:
($ref1,$ref2)=return_2_refs($input_ref1);

So that I don't have to create the extra $ref3 and $ref4?

( $ref1, $ref2 ) = map dclone( $_ ), return_2_refs( $input_ref1 );



John
 
A

anno4000

Graham Wood said:
I have a function that accepts an array reference as an argument and
returns 2 array references. I'm "dclone"ing the references so that when
I call the function again I'm not using the same references again. As in ...

I don't think that's necessary. If it is, there's probably something
wrong with the sub return_2_refs(). Unfortunately you aren't showing
the code.
=========================================================

($ref1,$ref2)=return_2_refs($input_ref1);
$ref3=dclone($ref1);
$ref4=dclone($ref2);

($ref5,$ref6)=return_2_refs($input_ref2);
=========================================================

Can anyone tell me how to incorporate the dclone into this line:
($ref1,$ref2)=return_2_refs($input_ref1);

( $ref1, $ref2) = map dclone( $_), return_2_refs($input_ref2);

should do that (untested).

Anno
 
G

Graham Wood

I don't think that's necessary. If it is, there's probably something
wrong with the sub return_2_refs(). Unfortunately you aren't showing
the code.

Here is the code for the function. Should I dclone something inside here?

Graham

sub construct_filepaths{

my @keys=qw(prod path filename version flag);
my @listing=@{$_[0]};
my %details;
my @paths, @missing;

foreach(@listing){
@details{@keys}=split();

my $filepath=dirname($bom)."/$details{prod}/".
"$details{path}/$details{filename}";

if(-f $filepath){
push(@paths,$filepath);
}
else{
push(@missing,$filepath);
}
}
return(\@paths,\@missing);
}
 
X

xhoster

Graham Wood said:
I don't think that's necessary. If it is, there's probably something
wrong with the sub return_2_refs(). Unfortunately you aren't showing
the code.

Here is the code for the function. Should I dclone something inside
here?

sub construct_filepaths{

my @keys=qw(prod path filename version flag);
my @listing=@{$_[0]};
my %details;
my @paths, @missing; ....
return(\@paths,\@missing);
}

There is no reason to use dclone either inside or outside that code.
It returns references to fresh arrays each time.


Xho
 
B

Brian McCauley

Graham said:
Graham Wood said:
I have a function that [...] returns 2 array references.
I'm "dclone"ing the references so that when
I call the function again I'm not using the same references again. As in ...

I don't think that's necessary. If it is, there's probably something
wrong with the sub return_2_refs(). Unfortunately you aren't showing
the code.

Here is the code for the function. Should I dclone something inside here?
sub construct_filepaths {
my @paths, @missing;
return(\@paths,\@missing);
}

No, there's nothing wrong there so no need for dclone.

Each time &construct_filepaths is called you get a new @paths and
@missing.

See FAQ: "Is it safe to return a reference to local or lexical data?"
 
J

J. Gleixner

Brian said:
Graham said:
I have a function that [...] returns 2 array references.
I'm "dclone"ing the references so that when
I call the function again I'm not using the same references again. As in ...
I don't think that's necessary. If it is, there's probably something
wrong with the sub return_2_refs(). Unfortunately you aren't showing
the code.
Here is the code for the function. Should I dclone something inside here?
sub construct_filepaths {
my @paths, @missing;
return(\@paths,\@missing);
}

No, there's nothing wrong there so no need for dclone.

Except for:

Graham, add the following to your code, to point out the issue with that
line.

use strict;
use warnings;
 
G

Graham Wood

Thanks to all for the responses. I've added some parentheses around

my(@found, @missing);

And it seems to be working the way I want it now. I'll file away the
map trick for future reference.

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top