-3
[root@ ~]$ perl -e "print 1 if blessed $a;"
1
[root@ ~]$ perl -e "print 1 if blessed $c;"
1
[root@ ~]$ perl -e "print 1 if blessed $cee;"
1

It seems always true,the version is 5.8.8.

UPDATE

I'm not running as root, it's CHANGED by me for the sake of privacy:)

asker
  • 2,159
  • 3
  • 22
  • 27

3 Answers3

10

blessed is not a keyword in Perl. You are using double quotes in your shell command, so the variables ($a, $c, etc.) are from your shell's environment, they are not Perl variables. Since these environment variables are probably empty, you are essentially executing the script

print 1 if blessed ;

When used like this, blessed is just a bareword string and always evaluates to true. What you have done is not much difference from running

$ perl -e 'print 1 if foo'
socket puppet
  • 3,191
  • 20
  • 16
8

Do you mean blessed from Scalar::Util? You probably want to load the function first:

perl -MScalar::Util=blessed -e "print 1 if blessed $a;"

otherwise your blessed is just bareword (string), which is obviously true.

bvr
  • 9,687
  • 22
  • 28
  • How can `blessed $a;` be interpreted as bareword? – asker Aug 18 '11 at 04:36
  • Bareword is not taken as string, as `print test` doesn't output `test`. – asker Aug 18 '11 at 04:52
  • @asker - `print test` is not good example, as it is parsed to `print test $_` (using test as filehandle). Try `print STDOUT test`. But anyway, in your case `blessed` is not bareword, because of surrounding context (it is interpreted as `$a->blessed` when blessed is not defined) – bvr Aug 18 '11 at 04:56
  • 5
    @asker, The shell interpolates an empty string for `$a`, so the code is just `print 1 if blessed `, which is the same as `print 1 if "blessed"` when strict is off. – ikegami Aug 18 '11 at 06:43
  • @bvr, It is NOT interpreted as `$a->blessed`. That would result in a runtime error. See my message to asker. – ikegami Aug 18 '11 at 06:44
  • @ikegami - sorry for confusion, on Windows it is and it indeed result in runtime error. – bvr Aug 18 '11 at 09:01
  • @bvr On my instance of Strawberry Perl (5.12) I get no runtime exception - it prints 1. Perhaps it's time to upgrade :) – Robert P Aug 18 '11 at 17:14
  • @Robert P, It has nothing to do with the OS, but the shell. @bvr is using `cmd` and I'm guessing you are using `bash`. – ikegami Aug 18 '11 at 18:44
  • @ikegami nope, I was using cmd on windows. Strawberry Perl is a windows only distribution :) – Robert P Aug 20 '11 at 04:48
  • @Robert P, No you weren't. Not if you ran the program you said you ran and got the output you said you got. Something you said isn't true. – ikegami Aug 20 '11 at 09:36
  • @Robert P, Furthermore, I never said you weren't using Windows. If you're on Windows, you're surely using cygwin's `bash`, not `cmd`. Although saying you're on Windows when using cygwin is rather disingenuous, since cygwin is a unix emulation layer. – ikegami Aug 20 '11 at 09:38
  • @Robert P - I am using quite recent AS perl 5.12.4. When you just run `perl -MO=Deparse -e "print 1 if blessed $a;"` within `cmd` shell, you get `print 1 if $a->blessed;`. Obviously `$a` is not interpolated by the shell. – bvr Aug 20 '11 at 13:33
  • @bvr: Interesting; now I can reproduce it with Strawberry Perl 5.12.2. Dunno what's going on. :) I think the real answer is: non-strict Perl is insane and should be avoided. – Robert P Aug 20 '11 at 20:05
1

As has been pointed out, you need to load the module before using the method. Also, if you had used perl -we instead of perl -e, you would probably not have asked this question.

For me, with perl -we, I get this warning:

Can't call method "blessed" without a package or object reference at -e line 1.
TLP
  • 66,756
  • 10
  • 92
  • 149