Hi Everyone,
I am currently reading up on the Camel I am unable to wrap my head around the following piece of code when paging through the chapter on Packages:
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6
7 sub fillerup ($);
8
9 my %fillme = (gas_pr => 3.59, gas_capacity => 14);
10
11
12 local $\ = "\n";
13
14 print '-'x80;
15 foreach my $key (keys %fillme ) {
16 print $key . '=' . $fillme{$key};
17 }
18
19 fillerup ( \%fillme );
20
21 print '-'x80;
22
23 foreach my $key (keys %fillme ) {
24 print $key . '=' . $fillme{$key};
25 }
26
27 sub fillerup ($) {
28 no strict;
29 local *hashref = shift;
30 # my $hashref = shift;
31
32 print "Prior to inserting a record in fillerup...";
33 print '*'x80;
34
35 foreach my $key (keys %$hashref ) {
36 print $key . '=' . $hashref->{$key};
37 }
38
39 $hashref->{filled_up} = 12.3;
40
41 print '*'x80;
42
43 foreach my $key (keys %$hashref ) {
44 print $key . '=' . $hashref->{$key};
45 }
46
47 print '*'x80;
48
49 return;
50 }
Now in the subroutine fillerup, I expect the auto-vivification of a new hash.
However, the output is as follows:
-------------------------------------------------------------
gas_capacity=14
gas_pr=3.59
Prior to inserting a record in fillerup...
**********************************************************************
+********
**********************************************************************
+********
filled_up=12.3
**********************************************************************
+********
-------------------------------------------------------------
gas_capacity=14
gas_pr=3.59
* Note that the output has been reformatted to promote visibility.
(1) Not sure why the locally created glob does not get the reference to the fillme hash. It does get a reference but one to an anonymous hash. If I comment out line 29 and uncomment line 30, I get the following output:
-------------------------------------------------------------
gas_capacity=14
gas_pr=3.59
Prior to inserting a record in fillerup...
**********************************************************************
+********
gas_capacity=14
gas_pr=3.59
**********************************************************************
+********
gas_capacity=14
filled_up=12.3
gas_pr=3.59
**********************************************************************
+********
-------------------------------------------------------------
gas_capacity=14
filled_up=12.3
gas_pr=3.59
* Note that the output has been reformatted to promote visibility.
(2) Also, the program fails to run when the
local qualifier on line 29 is changed to
our.
I have read through the tutorial on how local scoping works and I thought I was clear but apparently not.
I would appreciate your time and help if you could aid me in my understanding of the interaction of scoping and globs.
Thanks,
- Markov
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.