in reply to Sub calling sadness

ok( ThatPackage::blah $foo, 'blah' );
means
ok( ThatPackage::blah($foo, 'blah') );

Replies are listed 'Best First'.
Re^2: Sub calling sadness
by nefigah (Monk) on Mar 08, 2008 at 07:29 UTC
    ok( ThatPackage::blah $foo, 'blah' ); means ok( ThatPackage::blah($foo, 'blah') );
    Alright. Perl must fairly liberally interpret it sometimes; because it was working as ok( ThatPackage::blah($foo), 'blah' ) until $foo contained leading whitespace :( (I could tell because the ok() method was correctly printing out 'blah' as the test description)


    I'm a peripheral visionary... I can see into the future, but just way off to the side.
      Update: I wrote the below, obviously after reading ikegami's answer above. Yet, I went on, totally wrong. The only problem with his answer was that it's a bit terse. When the sub you are calling eats the rest of the argument list, you get surprises. For example, look at print. It seems a litle silly that ok() is OK with only one argument, but it is, which hides the real problem in your code.
      sorry for confusing... (e.g.:disregard the rest of this post)

      It is not possible to explain the semantics of your code without access to the definition of ThatPackage. If you could include that definition, we could probably better explain what happens and why.

      For example, include your definition of ThatPackage::blah something like this (after verifying that it still demonstrates the problem):

      use Test::More 'no_plan'; #require 'some_file_with_my_functions.pm'; package ThatPackage; sub blah { return $_[0] =~ /hi/ }; # Does NOT demonstrate the problem package main; my $foo = "hi"; my $bar = " hi"; ok( ThatPackage::blah $foo, 'blah' ); ok( ThatPackage::blah $bar, 'blah2' ); ok( ThatPackage::blah($foo), 'blah3' ); ok( ThatPackage::blah($bar), 'blah4' );
      cheers
        Fair enough, here it all is, shortened as short as possible:

        Contents of MyFile.pm
        #!/usr/bin/env perl use warnings; use strict; use feature ':5.10'; package ThatPackage; sub parse { } # Yep, even empty the error occurs, because it never eve +n gets as far as calling it
        Contents of unit_test.pl
        #!/usr/bin/env perl -w use feature ':5.10'; use Test::More 'no_plan'; require 'MyFile.pm'; my $foo = "blah"; my $bar = " blah"; ok( ThatPackage::parse $foo, " this is a test" ); # fine, test doesn't + pass but runs fine ok( ThatPackage::parse($bar), " this is a test" ); # also fine ok( ThatPackage::parse $bar, " this is a test" ); # error here: 'Can't + call method "ThatPackage::parse" without a package or object referen +ce at unit_test.pl line 43.'