Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `a::->func;` valid?

Tags:

perl

package a;
sub func {
print 1;
}
package main;
a::->func;

IMO it's enough to have a::func,a->func.

a::->func; looks very strange to me, why Perl supports this kind of strange looking syntax?

like image 823
asker Avatar asked Sep 05 '11 04:09

asker


People also ask

What makes a function a valid function?

To make sure that the function is valid, we need to check whether we get exactly one output for each input, and whether there needs to be any restriction on the domain.

How do you determine whether a function is valid?

Use the vertical line test to determine whether or not a graph represents a function. If a vertical line is moved across the graph and, at any time, touches the graph at only one point, then the graph is a function. If the vertical line touches the graph at more than one point, then the graph is not a function.

What does it mean for a function to be valid?

The VALID function determines whether x , a reference to a scalar pictured value, has a value that is valid with respect to its picture specification. The result is a bit string of length one that indicates if the character-string value of x can be edited into the picture declared for x .

What does function notation allow you to do why is this helpful?

Throughout mathematics, we find function notation. Function notation is a way to write functions that is easy to read and understand. Functions have dependent and independent variables, and when we use function notation the independent variable is commonly x, and the dependent variable is F(x).


2 Answers

To quote chromatic's excellent recent blog post on the topic at Modern Perl blog: "To avoid bareword parsing ambiguity."

To illustrate why such syntax is useful, here's an example evolved from your sample:

package a;
our $fh;
use IO::File;
sub s {
    return $fh = IO::File->new();
}

package a::s;
sub binmode {
    print "BINMODE\n";
}

package main;
a::s->binmode; # does that mean a::s()->binmode ?
               # calling sub "s()" from package a; 
               # and then executing sub "open" of the returned object?
               # In other words, equivalent to $obj = a::s(); $obj->binmode();
               # Meaning, set the binmode on a newly created IO::File object?

a::s->binmode; # OR, does that mean "a::s"->binmode() ?
               # calling sub "binmode()" from package a::s; 
               # Meaning, print "BINMODE"

a::s::->binmode; # Not ambiguous - we KNOW it's the latter option - print "BINMODE"
like image 160
DVK Avatar answered Sep 17 '22 17:09

DVK


a:: is a string literal that produces the string a. All the same:

 a->func()    # Only if a doesn't exist as a function.
 "a"->func()
 'a'->func()
 a::->func()
 v97->func()
 chr(97)->func()

etc

>perl -E"say('a', a, a::, v97, chr(97))"
aaaaa
like image 39
ikegami Avatar answered Sep 19 '22 17:09

ikegami