Perl weirdness 2010#001

Perl keeps impressing me (negatively) with weird stuff such as implicit context and so on....

but that one is really out of the ordinary...

prompt~/ perl 
print "-------\n\n";
my $x = 'hostname1.foo.net';
$x->{'datacenter'} ||= '__';
use Data::Dumper; 
print Dumper $x;
print "x".$x->{'datacenter'}."x\n";
my $y = 'hostname1.foo.neT';
print "y".$y->{'datacenter'}."y\n";
my $z = 'hostname1.foo.net';
print "z".$z->{'datacenter'}."z\n";
-------

$VAR1 = 'hostname1.foo.net';
x__x
yy
z__z
prompt~/ 

first, I thought calling ->{} on a scalar/string would crash. to my surprise, my program passed. so I wrote this test only to learning something even crazier about perl. couldn't this be a security issue?

Posted in , , , , | Posted on 30 Mar 2010 14:11by somekool | 2 comments

Comments

  1. Gabor Szabo Says

    You hit what is called a symbolic reference. It is a very powerful feature of Perl that if written unintentionally, can easily trip even the most experienced Perl programmers.

    To avoid accidental usage of that feature, the recommended way to write perl code is to start it with:

    use strict;

    Try to save your code in a file and precede it with the use strict; statement That will trigger a run time error:

    Can't use string ("hostname1.foo.net") as a HASH ref while "strict refs" in use at a.pl line 5.

  2. Gabor Szabo Says

    Better yet, write

    use strict;
    use warnings;
    

    That second line will give you all kinds of run-time warnings that indicate issues with your code.

Leave a comment

Subscribe

Personal Links

Tags

Categories

Archives

my Xbox 360 gamertag

my last twitter posts

Copyright © Mathieu Jobin's Life and Thoughts

Tech Blue designed by Hive Designs • Ported by Free WordPress Themes and Frédéric de Villamil Powered by Typo