Constructing a scalar reference

C

Chet Butcher

Hi

In the following sequence

$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.

Thanks
 
U

Uri Guttman

CB> In the following sequence

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

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

there is no special syntax to get a scalar ref like with hashes and arrays.

CB> my $r;
CB> mySub( \$r );

a do block works fine too:

my $r = do{ \my $r } ;

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

you need the \ somewhere to generate a scalar ref. and like with arrays
and hashes you can get them in loops and subs without the do block:

<untested pseudo code>

while( 1 )

my $foo = get_stuff() ;
push @foos, \$foo ;
}

perl will allocate a fresh scalar so you get a fresh scalar ref each
iteration. this is like:

while( 1 )

my @bar = get_list() ;
my %baz = get_hash() ;


push @stuff, { bar => \@bar, baz => \%baz } ;
}

you get new arrays and hashes each iteration because a ref to the old
value is still around (inside @stuff's hashes).

uri
 
S

Steve Roscio

There's not a special syntax for scalar refs, as there is for array refs
and hash refs. You can do this:

my $r = \"abc";
my $r = \undef;

etc...
 
D

Dr.Ruud

Chet said:
In the following sequence

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

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

$ perl -wle'
my $v = 1;
my $r = \$v;
$$r = 2;
print $v;
'
2
 
S

sln

Hi

In the following sequence

$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.

Thanks

Not sure that overloads can be done in Perl, maybe.
Wether the method expects a reference, and what type of reference, or not, is up to you.
Use alias parameter processing, calls can be general, figure out specifics in the method.
Then you could return success while processing data directly (one schema - are many more).

sln

-------------------------------------
sub ProcessData
{
return 0 if (@_ < 1);
my $Dataref;
if (!length( ref($_[0]) ) {
$Dataref = \$_[0];
} else {$Dataref = $_[0]}
shift;

if (ref($Dataref) eq 'SCALAR') {
return ProcessScalar($Dataref, @_);
}
if (ref($Dataref) eq 'ARRAY') {
return ProcessArray($Dataref, @_);
}
if (ref($Dataref) eq 'HASH') {
return ProcessHash($Dataref, @_);
}
return 0;
}

sub ProcessScalar
{
my ($scalar_ref, ...) = @_;
}
sub ProcessArray
{
my ($array_ref, ...) = @_;
}
sub ProcessHash
{
my ($hash_ref, ...) = @_;
}
 

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,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top