constructing a scalar reference

D

Dibosia

$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

my $r;
mySub( \$r );

I just want to use

my $r = (something);
mySub( $r );

to be consistent with

my $r = {}; # or my $r = [];
mySub( $r );

I know it's not a big drama on the surface, but I'm trying to overload
the method to return various results depending on the reference type,
and I dont want the \ in some calls and not others.

Thank-You
 
A

anno4000

Dibosia said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

my $r;
mySub( \$r );

I just want to use

my $r = (something);
mySub( $r );

to be consistent with

my $r = {}; # or my $r = [];
mySub( $r );

I know it's not a big drama on the surface, but I'm trying to overload
the method to return various results depending on the reference type,
and I dont want the \ in some calls and not others.

There is no such constructor.

do { \ my $x }

comes closest.

Anno
 
U

Uri Guttman

a> Dibosia said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

my $r;
mySub( \$r );

I just want to use

my $r = (something);
mySub( $r );

to be consistent with

my $r = {}; # or my $r = [];
mySub( $r );

I know it's not a big drama on the surface, but I'm trying to overload
the method to return various results depending on the reference type,
and I dont want the \ in some calls and not others.

a> There is no such constructor.

a> do { \ my $x }

a> comes closest.

depending on what the OP really needs (vs what he says he wants) you can
also use the aliasing of @_. pass in a scalar var and access its alias
in $_[0] (change 0 to the right slot). you can even take a ref to that
inside the sub like this:

sub bar {
my $ref = \$_[0] ;
${$ref} = 'foo' ; # changes the passed in var
}

my $var ;

bar( $var ) ;

$var is now 'foo'

the OP also never stated why or what will be done with the scalar
ref. it it is meant to be passed in in lieu of a ref to a scalar var (as
in anno's anon scalar ref), and not used after the call, then you can
just pass in undef, use the arg inside a lexical (assign from @_ as
normal) and scalar deref it. it will be autovivified to a scalar ref for
you. something like this:

sub foo {

my( $sref ) = @_ ;

${$sref} = 'foo' ; # autovivs if $sref is undef

# now use $sref for other stuff.

}

foo( undef ) ; # no external change
foo( \ my $bar ) ; # $bar is 'foo'

this is useful to either pass in a buffer you have and want to use or to
let the sub use its own buffer and throw it away when done.

uri
 
D

Dibosia

Dibosia said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

my $r;
mySub( \$r );

I just want to use

my $r = (something);
mySub( $r );

to be consistent with

my $r = {}; # or my $r = [];
mySub( $r );

I know it's not a big drama on the surface, but I'm trying to overload
the method to return various results depending on the reference type,
and I dont want the \ in some calls and not others.

There is no such constructor.

do { \ my $x }

comes closest.

Anno

Thanks to you and Uri. I thought perhaps there was something like this
that I was ignorant of. I guess what I was seeking was an "anonymous
scalar ref".

Seems like it might be handy in this case, but its not a difficult
workaround.
 
J

Josef Moellers

Dibosia said:
$r = {}; # a hashref
$r = []; # an arrayref

These are _anonymous_ references (i.e. the thingy referred to (the
"referent"?) has no name of its own).
$r = ?; # a scalar ref

AFAIK no _anonymous_ scalar references exist.
 
H

Heinrich Mislik

$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

Anything wrong with

$r = \undef;


Cheers

Heinrich
 
A

anno4000

Heinrich Mislik said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? I want to pass a ref to a scalar (pass by reference)
without resorting to

Anything wrong with

$r = \undef;

Yes, it's read-only. You won't be able to say "$$r = 123" later. For
that matter, you can't even bless it.

Anno
 
A

anno4000

Mumia W. said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? [...]

$r = \"This is a string.";

Like Heinrich Mislik's "\ undef" approach that results in a reference
to a read-only value. You can't assign to it and you can't bless it.
That makes it rather useless for most purposes.

Anno
 
B

Ben Morrow

Quoth Josef Moellers said:
Dibosia said:
$r = {}; # a hashref
$r = []; # an arrayref

These are _anonymous_ references (i.e. the thingy referred to (the
"referent"?) has no name of its own).
$r = ?; # a scalar ref

AFAIK no _anonymous_ scalar references exist.

Of course they do. Consider Anno's scalar-ref-constructor:

my $sref = do {\ my $x};

The $x name goes out of scope at the end of the do block, so thereafter
the scalar referred to by $sref has no name.

Ben
 
M

Mumia W.

Mumia W. said:
$r = {}; # a hashref
$r = []; # an arrayref
$r = ?; # a scalar ref

What is ? ? [...]
$r = \"This is a string.";

Like Heinrich Mislik's "\ undef" approach that results in a reference
to a read-only value. You can't assign to it and you can't bless it.
That makes it rather useless for most purposes.

Anno

You're right, it's best to just use an array ref with a single
element.
 
P

Paul Lalli

Mumia said:
You're right, it's best to just use an array ref with a single
element.

Huh? How is using an array ref with a single element an acceptable
solution to a taking a reference to a scalar?

Best is to do what's already been suggested in this thread as the
standard idiom:
my $ref = do { \my $x };

If you don't like the way that looks, you can always cover it with a
layer of indirection, by using Class::Std::Utils:

use Class::Std::Utils
my $ref = anon_scalar();

Paul Lalli
 

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,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top