Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\ operator in scalar context #805

Closed
p5pRT opened this issue Nov 3, 1999 · 5 comments
Closed

\ operator in scalar context #805

p5pRT opened this issue Nov 3, 1999 · 5 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 3, 1999

Migrated from rt.perl.org#1732 (status was 'resolved')

Searchable as RT1732$

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 1999

From mjtg@cus.cam.ac.uk

  If you prefer scalar context, similar chicanery is also useful for
  arbitrary expressions​:

  print "That yields ${\($n + 5)} widgets\n";

  Version 5.004 of Perl had a bug that gave list context to the
  expression in C<${...}>, but this is fixed in version 5.005.

But simple experiments show that the bug is still present in 5.005_62.
And the description of the bug is misleading​: ${...} correctly provides
scalar context; the bug is that the \ operator in scalar context still
provides list context to its argument.

I recall this being discussed on p5p long ago, but can't find it in
the archive.

Of course, we must ensure that

  $refa = \@​a;

does the Right Thing. Note that there are subtleties in
this area already, even in list context​:

DB<1> @​a = (4,5,6);

  DB<2> x \@​a, \(@​a)
0 ARRAY(0x1c26b4)
  0 4
  1 5
  2 6
1 SCALAR(0x19d684)
  -> 4
2 SCALAR(0x1c2630)
  -> 5
3 SCALAR(0x1c2624)
  -> 6
  DB<3>

So it seems that \@​a and \%a are special cases syntactically.

And BTW, why is the next sentence of the FAQ written

  See also ``How can I expand variables in text strings?'' in this
  section of the FAQ.

rather than as L<...> ?

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Nov 3, 1999

From @timbunce

On Wed, Nov 03, 1999 at 09​:45​:14AM +0000, M.J.T. Guy wrote​:

perlfaq4​: "How do I expand function calls in a string?" says

    If you prefer scalar context\, similar chicanery is also useful for
    arbitrary expressions&#8203;:

        print "That yields $\{\\\($n \+ 5\)\} widgets\\n";

    Version 5\.004 of Perl had a bug that gave list context to the
    expression in C\<$\{\.\.\.\}>\, but this is fixed in version 5\.005\.

But simple experiments show that the bug is still present in 5.005_62.
And the description of the bug is misleading​: ${...} correctly provides
scalar context; the bug is that the \ operator in scalar context still
provides list context to its argument.

That might have to be called a feature.

Tim.

@p5pRT
Copy link
Author

p5pRT commented Dec 13, 2000

From [Unknown Contact. See original ticket]

Someone should look into the rest of this report as well, but this solves
the pod "problem"​:

Inline Patch
--- perlfaq4.pod        Tue Oct 31 09:20:32 2000
+++ perlfaq4.new        Wed Dec 13 14:13:37 2000
@@ -430,8 +430,7 @@
 Version 5.004 of Perl had a bug that gave list context to the
 expression in C<${...}>, but this is fixed in version 5.005.
 
-See also ``How can I expand variables in text strings?'' in this
-section of the FAQ.
+See L</"How can I expand variables in text strings?">
 
 =head2 How do I find matching/nesting anything?
 
@@ -774,8 +773,7 @@
     );
     $text =~ s/\$(\w+)/$user_defs{$1}/g;
 
-See also ``How do I expand function calls in a string?'' in this section
-of the FAQ.
+See L</"How do I expand function calls in a string?">
 
 =head2 What's wrong with always quoting "$vars"?

@p5pRT
Copy link
Author

p5pRT commented Aug 7, 2002

@gbarr - Status changed from 'open' to 'resolved'

@p5pRT p5pRT closed this as completed Aug 7, 2002
@p5pRT
Copy link
Author

p5pRT commented Aug 7, 2002

From @gbarr

Its the () that puts the expression into a list context. The \() just
causes perl to create a list of references. the ${} then uses that list
in a scalar context, and a list used in a scalar context results in the
last element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant