Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Scalar alias vs. typeglob

by borisz (Canon)
on Oct 05, 2006 at 08:30 UTC ( #576487=perlquestion: print w/replies, xml ) Need Help??
borisz has asked for the wisdom of the Perl Monks concerning the following question:

I just want to alias a scalar to a different package var. But only the * notation work as expected. This looks like a bug to me, but propably someone can explain the behavior.

I expect, that both aliases do the same and the output is 'boris boris'
package XYZ; sub test { local *name = \'boris'; $_[1]->() } package ABC; *scalar_alias = \$XYZ::name; # does not work *glob_alias = *XYZ::name; # DWIM package main; use warnings; XYZ->test( sub { print 'scalar_alias: ', $XYZ::name, ' ', $ABC::scalar_alias, $/; print 'glob_alias: ', $XYZ::name, ' ', $ABC::glob_alias, $/; } ); __OUTPUT__ scalar_alias: boris glob_alias: boris boris Use of uninitialized value in print at untitled line 15.

Replies are listed 'Best First'.
Re: Scalar alias vs. typeglob
by ambrus (Abbot) on Oct 05, 2006 at 11:54 UTC

    The XYZ::test function has the statement local *name = \'boris';. That statement chagnes the scalar reference part of *XYZ::name not only the value of the scalar it points to. Now, the ABC package copies the old scalar reference to the *ABC::scalar_alias glob, so it won't see the new reference. (The same would be true if you changed that statement to local $name = 'boris'; because the local keyword also replaces the scalar reference.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://576487]
Approved by GrandFather
[james28909]: yep, i am scraping a websites date/time stamp from an article. none of that is relevant though because the strings i get are as expected. the returned data from the website is the list of @weekdays
[james28909]: i need to convert those @weekdays to an numerical date based off of scalar localtime
[james28909]: i have thught about this and though about it lol
[james28909]: also remember, that if 'Today' is may 1, then Yesterday is not may 31 lol
[james28909]: theoretically speaking ofcourse
[james28909]: so if you subtracting as you loop, be aware that you will eventually be subtracting into a past month, or year <.<
[james28909]: dontcha love it!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-04-29 03:53 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.