nikolay has asked for the wisdom of the Perl Monks concerning the following question:

In the following code i want to dereference the array's element $a[0].
my $q='qwer'; my @a=( \$q );
How to do that? Using additional variable i do it so
my $a=$a[0]; print "$a\n";
How to do the same w/o additional variable. Reading PERL documentations did not reveal me the secret of syntax of the case. Thanks for advice.

Replies are listed 'Best First'.
Re: How to dereference array of ref.s?
by Discipulus (Canon) on May 20, 2018 at 14:46 UTC
    Hello nikolay

    you can dereference using braces and prepending the right sigyl;  print ${ $a[0] }

    Standard doc is perlref

    Your array element is just a plain scalar and you must learn just how to derefence a sclar. Infact is the same of:  my $ref = \'qwerty'; print ${ $ref }

    If no confusion is possible, as in my second example, you can dereference without braces like in  print $$ref or using postfix dereference  use feature 'postderef'; my $ref = \'qwerty'; print $ref->$* but see the above docs for versions of perl that have this feature enable or usable.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      versions of perl that have this feature enable or usable

      The Postfix Dereference Syntax is enabled by default in Perl 5.24 and above. E.g. on v5.26:

      $ perl -wMstrict -e' my $q="qwer"; my @a=(\$q); print $a[0]->$*,"\n";' qwer

      However, if you want it to interpolate in strings, you do need to enable the postderef_qq feature, for example via the v5.24 feature bundle ("use 5.024;"):

      $ perl -wM5.024 -e' my $q="qwer"; my @a=(\$q); print "$a[0]->$*\n";' qwer
      Thank you! Awesome!
Re: How to dereference array of ref.s?
by Arunbear (Prior) on May 20, 2018 at 14:49 UTC
    % perl -dee Loading DB routines from perl5db.pl version 1.49_05 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): e DB<1> $q='qwer' DB<2> @a=( \$q ) DB<3> x @a 0 SCALAR(0x1158960) -> 'qwer' DB<4> p ${$a[0]} qwer
    So essentially
    print ${ $scalar_ref };
    See also References quick reference
      Great!
Re: How to dereference array of ref.s?
by johngg (Canon) on May 20, 2018 at 14:53 UTC

    This is also useful.

    Cheers,

    JohnGG