[albany-pm] Typeglobs help!

Mark Fowler mark at twoshortplanks.com
Thu Mar 27 19:38:38 PDT 2014


Ah Patrick, I've got bad news for you...

You're mixing up typeglobs and scalars. A typeglob is essentially a symbol
table entry that holds everything with different sidles.  So the *foo
typeglob holds $foo, @foo, &foo, %foo, etc, etc.

A scalar is little C struct[1] that holds a integer value, a floating point
value, a pointer to a string, etc etc and a set of flags indicating which
of these are valid or not. Whenever Perl needs a different kind of value -
say the sting form of some scalar computed by a numerical calculation - it
converts the value from one of the other forms and stores it back in the
scalar and marks the flags to indicate that right now both values are good
and Perl doesn't need to do any conversion.

A dualvar is essentially a scalar where you can manually set the string
part and numerical part of the scalar by hand to different values.

So, no we can't use typeglobs because typeglobs only have one entry for a
scalar in them, not a separate scalar for numerical and string values.

To make matters worse, there's no way as far as I know to access the
various parts of a scalar from Perl space without using some XS code.

Possible further solutions:

- ask for help with Net::SSH
- shell out to the system ssh binary, possibly using an Expect command if
you need to
- install a custom Perl just for this task. perlbrew is your friend here.

Mark

[1] technically a set of structs depending on how it's being used, but
that's not important right now.

On Thursday, March 27, 2014, Patrick Cronin <oshihuna at gmail.com> wrote:

> I manage a CentOS 5.10 server running perl 5.8.8. For <REASONS>, I can't
> upgrade it.
>
> For some reason, I can't install the XS version of Scalar::Util, and so
> I'm stuck using the PP version. Net::OpenSSH relies on Scalar::Util's
> dualvar function (which is only available in the XS version of
> Scalar::Util) to create a single scalar holding both an integer and string
> value. When the scalar is used in a numeric context, the integer value
> comes out, and when the scalar is used in a string context, the string
> value comes out. I'm trying to write a quick and dirty PP version of
> dualvar so I can move on with my life.
>
> I seem to remember seeing this being done, but no amount of googling is
> getting me there. I've looked up typeglobs and the *foo{THING} construct,
> but while THING can be SCALAR, it can't be NUMBER or STRING. Does anyone
> know the magic to make this happen?
>
> I'm trying variations of the following:
>
> my $int = 2;
> my $string = "test";
>
> my $typeglob = $int;
> *typeglob{SCALAR} = ${ *string };
>
> print ($typeglob + 2) . "\n";
> print $typeglob . " YES!\n";
>
> It just feels gross, and it's not working.
>
> Does anyone have the magic?
>
> ------------------------------
> [image: Patrick's Avatar]*Patrick Cronin*
> Computer Lover
> Mobile: +1 518 336 5133
> Email: oshihuna at gmail.com<javascript:_e(%7B%7D,'cvml','oshihuna at gmail.com');>
> Skype: patrickcronin12061
>
> P Please consider the environment before printing this e-mail /
> attachments
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/albany-pm/attachments/20140327/2bba58bd/attachment.html>


More information about the Albany-pm mailing list