Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Constructing a scalar reference

Reply
Thread Tools

Constructing a scalar reference

 
 
Chet Butcher
Guest
Posts: n/a
 
      01-30-2009
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

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      01-30-2009
>>>>> "CB" == Chet Butcher <> writes:

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

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
Steve Roscio
Guest
Posts: n/a
 
      01-31-2009
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...
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-31-2009
Chet Butcher wrote:

> 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

--
Ruud
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      01-31-2009
On Fri, 30 Jan 2009 22:47:53 +0000, Chet Butcher <> wrote:

>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, ...) = @_;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructing a scalar reference Chet Butcher Perl Misc 3 06-09-2009 06:13 AM
constructing a scalar reference Dibosia Perl Misc 11 08-22-2006 04:05 PM
object reference handle (like perl's reference to scalar) Eric Mahurin Ruby 4 05-06-2005 05:30 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57