Constructing a scalar reference

Discussion in 'Perl Misc' started by Chet Butcher, Jan 30, 2009.

  1. Chet Butcher

    Chet Butcher Guest

    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
     
    Chet Butcher, Jan 30, 2009
    #1
    1. Advertising

  2. Chet Butcher

    Uri Guttman Guest

    >>>>> "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 ---------
     
    Uri Guttman, Jan 30, 2009
    #2
    1. Advertising

  3. Chet Butcher

    Steve Roscio Guest

    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...
     
    Steve Roscio, Jan 31, 2009
    #3
  4. Chet Butcher

    Dr.Ruud Guest

    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
     
    Dr.Ruud, Jan 31, 2009
    #4
  5. Chet Butcher

    Guest

    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, ...) = @_;
    }
     
    , Jan 31, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Mahurin
    Replies:
    4
    Views:
    256
    David A. Black
    May 6, 2005
  2. Clint Olsen
    Replies:
    6
    Views:
    380
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    171
    Arndt Jonasson
    Jan 27, 2005
  4. Dibosia

    constructing a scalar reference

    Dibosia, Aug 21, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    185
    Paul Lalli
    Aug 22, 2006
  5. Chet Butcher

    Constructing a scalar reference

    Chet Butcher, Jun 7, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    101
    Phil Carmody
    Jun 9, 2009
Loading...

Share This Page