Despite what you might be thinking, I'm not talking about printing to a filehandle. However, whileI was trying to help a coworker understand variable interpolation in strings, I came up with the following code:

#!/usr/bin/perl use strict; use warnings; use Test::More tests => 6; my %french_for = ( one => 'un', ); my $num = 'one'; is "$french_for{one}", 'un', 'bare literal key'; is "$french_for{'one'}", 'un', 'single quoted literal key'; is qq[$french_for{"one"}], 'un', 'double quoted literal key'; is "$french_for{$num}", 'un', 'bare variable key'; is "$french_for{'$num'}", 'un', 'single quoted variable key'; is qq[$french_for{"$num"}], 'un', 'double quoted variable key';

The "$french_for{'$num'}" doesn't work because, as hv explained on P5P:

A variable access is parsed as code. "'$num'", the variable being accessed is $num; in "$french_for{'$num'}", the access is to: $french_for{'$num'} which is a nonexistent hash element. I'm not sure what behaviour you were expecting instead, but I've never noticed anyone stumble on this before.

Note that this is precisely what allows you to use tricks like "@{[ 2 + 2 ]}" or "${\( 2 + 2 )}" to interpolate code in a string.

And this allowed me to come up with this:

temp $ touch foo.bar temp $ ls foo.bar temp $ perl -Te '%ENV = (); print "$ENV{`rm foo.bar`}"' temp $ ls temp $

Note that "foo.bar" is now gone. Frankly, I think you'd have to jump through a few hoops to create a security hole here, but I thought it was interesting.

Cheers,
Ovid

New address of my CGI Course.

Formating fixed by Me


In reply to How to delete a file with a print statement by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.