http://www.perlmonks.org?node_id=1003864

jonagondos has asked for the wisdom of the Perl Monks concerning the following question:

why doesn't this piece code run correctly at runtime i probably dont understand the small arrow ->? compile error during runtime is Scalar::Util::looks_like_number(sv) at test.pl line 16, <STDIN> line 1.

if ($numeral = Scalar::Util->looks_like_number($user_radius)) { full code
#!/usr/bin/perl use warnings; use Scalar::Util; $radius_times = 2; $radius_times_2 = 3.141592654; $radius_times_2 *= $radius_times; while (1) { print "enter the radius\n"; $user_radius = <STDIN>; chomp($user_radius); if (my $numeral = Scalar::Util->looks_like_number($user_radius)) { $user_radius *= $radius_times_2; print "circumference is equal to $user_radius\n"; } else { print "no user input\n"; } }

Replies are listed 'Best First'.
Re: runtime problem
by toolic (Bishop) on Nov 14, 2012 at 17:27 UTC
    Use :: instead of ->
    if (my $numeral = Scalar::Util::looks_like_number($user_radius)) {

    Scalar::Util does not have an object-oriented interface.

    Similarly, you can also import the sub:

    use Scalar::Util qw(looks_like_number); ... if (my $numeral = looks_like_number($user_radius)) {
Re: runtime problem
by tobyink (Canon) on Nov 14, 2012 at 18:36 UTC

    toolic is correct. This is cool though...

    sub FakeOO { eval "require $_[0]"; bless \($_[0]), "FakeOO"; } sub FakeOO::AUTOLOAD { my $pkg = ${+shift}; $pkg->can($FakeOO::AUTOLOAD =~ /::(\w+)$/)->(@_); } my $scalar_util = FakeOO("Scalar::Util"); $scalar_util->looks_like_number(123);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Yes, pretty cool.

      And it also pretty much sums up why Perl is so loved and hated by so many people. You can do any insane, ill-advised thing you want :)



      When's the last time you used duct tape on a duct? --Larry Wall
      thanks guys