[Israel.pm] List of all contexts in Perl

Yuval Kogman nothingmuch at woobling.org
Sun May 30 09:15:46 PDT 2010

On 30 May 2010 13:32, Mikhael Goikhman <migo at homemail.com> wrote:

> On 30 May 2010 12:09:52 +0300, Offer Kaye wrote:
> >
> > I'm not sure I understand what "boolean context" means, or any of the
> > others you listed. Except for the ".." operator does any of Perl's
> > built-in functions operate differently inside an "if" test for
> > example, in terms of the value returned (assuming you assign it to
> > something)?
> >
> > Looked at another way, you can do anything you want with the return
> > value of for example "localtime", but in the end it will still return
> > either a scalar or list, regardless of what you do with those values
> > later on, right?
> In fact localtime works differently under scalar, boolean (works like
> numerical here) and list contexts. Run this if you don't believe me:
>        perl -le 'print scalar localtime'
>        perl -le 'print int localtime'
>        perl -le 'print localtime == 0 ? "TRUE" : "FALSE"'
>        perl -le 'print join ", ", localtime'
> But most of other built-in functions may work the same under numeric,
> scalar, boolean or say dereferencing contexts. This does not mean
> these contexts do not exist. :)

Scalars can have two values (see Scalar::Util::dualvar) which is how $! etc
are implemented.

However, localtime doesn't do that, at least not on my machine:

$ int localtime
Argument "Sun May 30 19:14:06 2010" isn't numeric in int at (eval 356) line

Otherwise all the dereferencing contexts, boolean stuff etc are just scalar
context with a well defined operation applied to the scalar afterwords.

Many operations in Perl are implicit (e.g. the SvIV macro which takes the
integer value of an SV can do anything from simply returning the IV slot to
calling object method on the SV due to overloading), but it's more accurate
to think of these as actual functions applied to data.

It's a transformation that is applied to the returned value.

Note that putting a value in a variable results in a copy so some semantics
(i.e. SV magic) are not carried over (which is why you need to use
references to tied variables to retain tiedness, for instance).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20100530/2e4c5d10/attachment.htm 

More information about the Perl mailing list