Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:39:25+00:00 2026-06-04T09:39:25+00:00

Consider the following simple piece of code: %hash = (‘a’=>1,’b’=>2); print $hash{‘b’}; print \n,(\%hash)->{‘b’};

  • 0

Consider the following simple piece of code:

%hash = ('a'=>1,'b'=>2); 
print $hash{'b'};
print "\n",(\%hash)->{'b'};   #used when hashes are passed by reference
                              #to subroutines

The output, as expected, is a pair of 2’s. But I was thinking whether $hash{key} is a shorthand for the referencing & dereferencing done as (\%hash)->{key}, or is it an entirely different route to reach the same result.

Please give some clarification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:39:26+00:00Added an answer on June 4, 2026 at 9:39 am

    They’re somewhat different because unlike many other languages, where all complex types are only available as references, Perl have actual plain hash type and separate reference type that can act as proxy to any other type. You can find gory details about this in perlguts.

    In the end those two examples both pull data from same storage, of course, but the second invocation is a bit longer because it spends time dutifully creating a reference to plain HV and then dereferencing it back just as you’ve asked. You can study details on what going under the hood using B::Concise module.

    %hash = ('a'=>1,'b'=>2);
    print $hash{'b'};
    print (\%hash)->{'b'};
    

    Concise output:

    $ perl -MO=Concise deref.pl 
    t  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 1 deref.pl:1) v:{ ->3
    b     <2> aassign[t3] vKS ->c
    -        <1> ex-list lKP ->8
    3           <0> pushmark s ->4
    4           <$> const[PV "a"] s ->5
    5           <$> const[IV 1] s ->6
    6           <$> const[PV "b"] s ->7
    7           <$> const[IV 2] s ->8
    -        <1> ex-list lK ->b
    8           <0> pushmark s ->9
    a           <1> rv2hv[t2] lKRM*/1 ->b
    9              <#> gv[*hash] s ->a
    c     <;> nextstate(main 1 deref.pl:2) v:{ ->d
    i     <@> print vK ->j
    d        <0> pushmark s ->e
    h        <2> helem sK/2 ->i
    f           <1> rv2hv sKR/1 ->g
    e              <#> gv[*hash] s ->f
    g           <$> const[PV "b"] s ->h
    j     <;> nextstate(main 1 deref.pl:3) v:{ ->k
    s     <2> helem vK/2 ->t
    q        <1> rv2hv[t7] sKR/1 ->r
    p           <@> print sK ->q
    k              <0> pushmark s ->l
    o              <1> refgen lK/1 ->p
    -                 <1> ex-list lKRM ->o
    l                    <0> pushmark sRM ->m
    n                    <1> rv2hv[t6] lKRM/1 ->o
    m                       <#> gv[*hash] s ->n
    r        <$> const[PV "b"] s ->s
    deref.pl syntax OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: print 3 ** 333; #Yields 7.6098802313206e+158 My question is simple: How
What is equivalent pythonic implementation for the following simple piece of code in Matlab.
Consider the following simple python code >>> L = range(3) >>> L [0, 1,
Consider the following simple code to create a typesafe equals. This first section allows
Consider the following simple code: List <?> list4[] = { Arrays.asList(1,2), Arrays.asList(1,2)}; I do
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
Consider the following piece of mathematica code: a := {1, 2, 3}; f[n_, a_]
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following simple code: GetDlgItemText(IDC_EName,LPTSTR(cName),11); k=strlen(cName); I want to get the length of
Let's consider the following simple code in Java. package temppkg; interface Interface { @Override

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.