← Back to Perl Course | Chapter 6: References | Lesson 1 of 7

Scalar References का परिचय

एक reference एक scalar है जो याद रखता है कि एक दूसरा variable कहाँ रहता है।

In this page:

  1. Scalar References

Scalar References

backslash operator एक variable का एक reference बनाता है, इसलिए \$x $x की ओर point करता है। आप reference को एक extra dollar sign से follow, या dereference, करते हैं जैसे $$ref में, या arrow-free block form ${$ref} से। reference के जरिए value बदलना original variable बदल देता है, क्योंकि दोनों same storage को नाम देते हैं।

Note: ref function एक reference का type return करता है, जैसे SCALAR, ARRAY, HASH, या CODE, और एक non-reference के लिए एक empty string।

उदाहरण: Scalar references

perl
use strict;
use warnings;

my $x = 10;
my $ref = \$x;
print "value through ref: $$ref\n";
$$ref = 20;
print "x is now $x\n";
${$ref} += 5;
print "x after += : $x\n";
print "ref type: ", ref($ref), "\n";
print "ref of plain value: '", ref($x), "'\n";
my $ref2 = $ref;
print "same target: ", ($ref == $ref2 ? "yes" : "no"), "\n";

# Output:
# value through ref: 10
# x is now 20
# x after += : 25
# ref type: SCALAR
# ref of plain value: ''
# same target: yes
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक reference print करना और इसकी value की उम्मीद करना
  2. dereference करते वक्त extra $ भूल जाना
  3. यह मान लेना कि एक reference data की एक copy है
चैप्टर सारांश
  • \$x एक reference बनाता है
  • $$ref इसे dereference करता है
  • एक reference के जरिए changes original बदल देते हैं
  • ref reference type return करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.