|
Posted by mumebuhi on August 30, 2006, 5:20 pm
Please log in for more thread options > > I am trying to understand the differences among these constructs:
> >
> > # 1
> > my $p : shared;
> > $p-> = "My Name";
> >
> > # 2
> > my $p : shared = &share({});
> > $p-> = "My Name";
> >
> > Do 1 and 2 achieve the same thing?
>
> No. 1 gives a "Invalid value for shared scalar" error in nontrivial
> contexts. 2 doesn't (but overriding prototypes is probably bad form,
> anyway).
>
> # 2.5
> my $p = share(%});
> $p-> = "My Name";
>
> # 2.75
> my $p={}; share %$p;
> $p-> = "My Name";
>
> >
> > # 3
> > my %p : shared;
> > $p = "My Name";
> >
> > Is it fair to say that 3 is the simpler form compared to 1 and 2?
>
> In the sense that it is simpler to not use gratuitous references than it is
> to use gratuitous references, sure, 3 is simpler. But using the same
> logic, we could say it is even simpler not to share the variables at all,
> as you don't make meaningful use of the shared nature. Whether you need
> the complexity of sharing, or need the complexity of references, depends on
> the parts of your code you haven't shown us.
I tend to use #3 for a regular usage. However, when I need to construct
a new object, that will be shared by several threads, I have to use #2
in the constructor. Is this reasonable?
|