Why do the old the old avatars remain after being replaced by the new ones?
typeglobs and references
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > Perl web-programming > typeglobs and references 25 July 2001 18:39:40

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

typeglobs and references

Silvio Luis Leite Santana 25 July 2001 18:39:40
 Hi
I have been reading the camel book; it's really fantastic
(and funny), but I still didn't understand some topics.
One thing that's not so clear is about constants made with
typeglobs and references; that's in page 295 of 3rd Ed.
I have made the following program to test them:

#!/usr/bin/perl -w
use strict;

our $PI;

$PI = \3.1415926535;
print "(1) $PI\n";
# prints SCALAR(0x8101f64)

$PI = 7;
print "(2) $PI\n";
# prints 7

*PI = \4.31415926;
print "(3) $PI\n";
# prints 4.31415926

$PI = \5.31419526;
# error: Modification of a read-only value attempted at ./m2 line 18.
print "(4) $PI\n";

And now the questions:

Question 1) Ignoring the different constant values,
Is the first and the third attributions equivalent?
It seems that not, but then, why?
I thought that when I wrote
*PI = \value
I was giving the scalar $PI the value "reference to a value",
and not the value itself (that is what's being printed)!?

Question 2) Why cannot we make the 4th attribution? What is
making the scalar $PI a read-only? It cannot be the
previous reference, because we also set it to a reference on the
first attribution, but we could change it on the second...

Thanks in advance and for a long time
Silvio Santana
Add comment
James Kipp 24 July 2001 20:52:25 permanent link ]
 
$PI = \3.1415926535;> print "(1) $PI\n";> # prints SCALAR(0x8101f64)

because it is not dereferenced so the value is the memory location.
$PI = 7;> print "(2) $PI\n";> # prints 7

like it should
*PI = \4.31415926;> print "(3) $PI\n";> # prints 4.31415926\

the typeglob provides in this case provides an an auto dereference and
forces a read only (constant) variable. this is similar to inlining
functions.
sub PI () {4.3145926}
is doing the same thing. the function is prototyped to take no args, setting
a constant value to be returned.>
$PI = \5.31419526;> # error: Modification of a read-only value attempted at ./m2 line 18.> print "(4) $PI\n";

see above !!
And now the questions:>
Question 1) Ignoring the different constant values,> Is the first and the third attributions equivalent?> It seems that not, but then, why? > I thought that when I wrote> *PI = \value> I was giving the scalar $PI the value "reference to a value",> and not the value itself (that is what's being printed)!?>
Question 2) Why cannot we make the 4th attribution? What is> making the scalar $PI a read-only? It cannot be the > previous reference, because we also set it to a reference on the> first attribution, but we could change it on the second...>
Thanks in advance and for a long time> Silvio Santana>
-- > To unsubscribe, e-mail: beginners-unsubscri­be@perl.org> For additional commands, e-mail: beginners-help@perl­.org>

Add comment
Michael Fowler 24 July 2001 20:54:13 permanent link ]
 On Tue, Jul 24, 2001 at 02:32:18PM -0300, Silvio Luis Leite Santana wrote:> #!/usr/bin/perl -w> use strict;>
our $PI;>
$PI = \3.1415926535;> print "(1) $PI\n";> # prints SCALAR(0x8101f64)>
$PI = 7;> print "(2) $PI\n";> # prints 7>
*PI = \4.31415926;> print "(3) $PI\n";> # prints 4.31415926>
$PI = \5.31419526;> # error: Modification of a read-only value attempted at ./m2 line 18.> print "(4) $PI\n";>
And now the questions:>
Question 1) Ignoring the different constant values,> Is the first and the third attributions equivalent?

I'm not sure how you're using "attribution" here. Do you mean assignment?

No, the first ($PI = \3.1415926535) and third (*PI = \4.31415926)
assignments are not equivalent, as evidenced by the value you get back when
you print it. The first assignment is assigning a scalar reference to $PI,
the third is filling the scalar slot of the PI glob with the value
4.31415926.

It seems that not, but then, why? > I thought that when I wrote> *PI = \value> I was giving the scalar $PI the value "reference to a value",> and not the value itself (that is what's being printed)!?

You aren't, you are filling the scalar slot of the PI glob with a value. If
that value is a constant then $PI is read-only, because you can't change a
constant.

Question 2) Why cannot we make the 4th attribution? What is> making the scalar $PI a read-only?

The third assignment makes it read-only.

It cannot be the previous reference, because we also set it to a reference> on the first attribution, but we could change it on the second...

It can be on the previous reference because "$PI = \3.1415926535" is very
different from "*PI = \4.31415926".


Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com­
--
Add comment
Silvio Luis Leite Santana 25 July 2001 17:57:08 permanent link ]
 Thank you very much for your help
about Perl.

There's just something I didn't get yet.
When I write

*PI = 5;

What am I really doing?
I think I was assigning (not attribing :)­ ) the value
5 to the scalar slot of the glob PI, since 5 is scalar,
but when asking for a

print "$PI";

all we see is a
'Use of uninitialized value in concatenation (.)'
message, even if $PI is being previously
declared with my or our.

Thanks again
Silvio Santana
Add comment
Paul 25 July 2001 18:13:16 permanent link ]
 
--- Silvio Luis Leite Santana <silvio@ahand.unica­mp.br> wrote:> Thank you very much for your help> about Perl.>
There's just something I didn't get yet.> When I write>
*PI = 5;>
What am I really doing?

I think you meant

*PI = \5;

Which is assigning the glob PI a reference to a literal scalar, which
will be therefore stored in the scalar slot of the glob.
That means that $PI will now have an UNEDITABLE value of 5 (though you
could still reassign to *PI if you wanted....)


___________________­____________________­___________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.ya­hoo.com/
Add comment
Jeff 'japhy/Marillion' Pinyan 25 July 2001 18:19:12 permanent link ]
 On Jul 25, Silvio Luis Leite Santana said:
When I write>
*PI = 5;>
What am I really doing?

Well, you can assign a string to a typeglob, and Perl will assume that you
meant to assign a typeglob to a typeglob:

*first = *1;
"japhy" =~ /([aeiou])/;
print "$first\n"; # prints 'a'

--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.co­m/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonk­s.com/ http://forums.perlg­uru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetr­ics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **

Add comment
Jeff 'japhy/Marillion' Pinyan 25 July 2001 18:39:40 permanent link ]
 On Jul 25, Jeff 'japhy/Marillion' Pinyan said:
On Jul 25, Silvio Luis Leite Santana said:>
When I write>>
*PI = 5;>>
What am I really doing?>
Well, you can assign a string to a typeglob, and Perl will assume that you>meant to assign a typeglob to a typeglob:>
*first = *1;> "japhy" =~ /([aeiou])/;> print "$first\n"; # prints 'a'

Let me explain a little more fully: EVERY '1' variable now has an alias
in a 'first' variable:

*first = 1; # is like *first = *1

push @1, "foo";
print $first[0]; # 'foo';
push @first, "bar";
print $1[1]; # 'bar';


$1{abc} = 'def';
print $first{abc}; # 'def'
$first{gh} = 'ij';
print $first{gh}; # 'ij'

# and so on...

You might want to read:

http://www.pobox.co­m/~japhy/articles/pm­/2000-03.html

--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.co­m/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonk­s.com/ http://forums.perlg­uru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetr­ics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **

Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > Perl web-programming > typeglobs and references 25 July 2001 18:39:40

see also:
CVS Account Request: dmitry83
CVS Account Request: pjsky
php 4.2.2
пройди тесты:
see also:
hello
Тест: Do you really know yourself?i...
how are you, people?

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .