For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > context?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author context?
Joel

2008-03-18, 10:08 pm

when calling functions, we need not include parantheses, and we're
still able to pass a list..
like for eg:
my_function "foo", "bar"; #this copies foo and bar into @_ of
my_function.

but alternately the same assignment like @_ = "foo", "bar";
will omit "bar" from the list.. why is this?

Thanks,
Joel

John W. Krahn

2008-03-18, 10:08 pm

Joel wrote:
> when calling functions, we need not include parantheses, and we're
> still able to pass a list..
> like for eg:
> my_function "foo", "bar"; #this copies foo and bar into @_ of
> my_function.
>
> but alternately the same assignment like @_ = "foo", "bar";
> will omit "bar" from the list.. why is this?


That is the way that the comma operator works. "foo" is evaluated and
then discarded and then "bar" is evaluated and the result returned.
Because the comma operator has lower precedence then the assignment
operator you have to enclose the right hand side in parentheses;

@_ = ( "foo", "bar" );


In the case of:

my_function "foo", "bar";

my_function is a list operator which has a very low precedence so the
parentheses are not required.


perldoc perlop
perldoc perlsub



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Gunnar Hjalmarsson

2008-03-18, 10:08 pm

John W. Krahn wrote:
> In the case of:
>
> my_function "foo", "bar";
>
> my_function is a list operator which has a very low precedence so the
> parentheses are not required.


Sometimes, i.e. if the sub is not predeclared, they are required.

C:\home>type test.pl
my_function "foo", "bar";
sub my_function { print "@_\n" }

C:\home>perl test.pl
String found where operator expected at test.pl line 1, near
"my_function "foo""
(Do you need to predeclare my_function?)
syntax error at test.pl line 1, near "my_function "foo""
Execution of test.pl aborted due to compilation errors.

C:\home>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Joel

2008-03-20, 7:02 pm

> > my_function "foo", "bar";
>
>
> Sometimes, i.e. if the sub is not predeclared, they are required.
>


yes this is true, because perl doesn't know that my_function is
actually a function call when it doesn't see it predeclared..

I have another question, why are function calls without parantheses
called list operators, is there no difference between the two?
why isn't a function call with parantheses called a list operator too?

Thanks,
Joel

Chas. Owens

2008-03-20, 7:02 pm

On Thu, Mar 20, 2008 at 11:28 AM, Joel <agnel.joel@gmail.com> wrote:
>
> yes this is true, because perl doesn't know that my_function is
> actually a function call when it doesn't see it predeclared..
>
> I have another question, why are function calls without parantheses
> called list operators, is there no difference between the two?
> why isn't a function call with parantheses called a list operator too?

snip

Not all subroutines are list operators. For instance,

sub square ($) { $_[0] ** 2 }

is a named unary operator.

from perldoc perlop*
Actually, there aren't really functions in this sense, just
list operators and unary operators
behaving as functions because you put parentheses around the arguments.

* http://perldoc.perl.org/perlop.html...List-Operators-(Leftward)
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Joel

2008-03-21, 7:04 pm

Can you tell me the sequence of events that happen (internally in perl
during parsing) for the following code:

sub fun
{
print @_;
}

fun fun (1), fun 2, fun (3);

I am particularly interested in the comma operator, in the above code,
as I understand it
(1) first the list operations (which have highest precedence because
of parantheses) will execute first printing "13".
(2) then the comma between fun(1) and fun 2 is evaluated, once this
happens, is the list operation ( fun 2, fun(3) ) evaluated or is the
comma between 2 and fun(3) evaluated?

Thanks,
Joel

On Mar 20, 9:21=A0pm, chas.ow...@gmail.com (Chas. Owens) wrote:
> On Thu, Mar 20, 2008 at 11:28 AM, Joel <agnel.j...@gmail.com> wrote:
>
the[color=darkred]
>
>
>
?[color=darkred]
>
> snip
>
> Not all subroutines are list operators. =A0For instance,
>
> sub square ($) { $_[0] ** 2 }
>
> is a named unary operator.
>
> from perldoc perlop*
> =A0 =A0 =A0 =A0Actually, there aren't really functions in this sense, just=


> list operators and unary operators
> =A0 =A0 =A0 =A0behaving as functions because you put parentheses around th=

e arguments.
>
> *http://perldoc.perl.org/perlop.html...List-Operators-(Leftward)
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.


Rob Dixon

2008-03-21, 7:04 pm

Joel wrote:
>
> Can you tell me the sequence of events that happen (internally in perl
> during parsing) for the following code:
>
> sub fun
> {
> print @_;
> }
>
> fun fun (1), fun 2, fun (3);
>
> I am particularly interested in the comma operator, in the above code,
> as I understand it
> (1) first the list operations (which have highest precedence because
> of parantheses) will execute first printing "13".
> (2) then the comma between fun(1) and fun 2 is evaluated, once this
> happens, is the list operation ( fun 2, fun(3) ) evaluated or is the
> comma between 2 and fun(3) evaluated?


I suspect you are forgetting that fun() itself returns the success
status of the print() function, introducing additional 1s into the
output. Look:

local $" = ',';

sub fun {
print "fun(@_) ";
}

fun fun ('a'), fun 'b', fun ('c');

**OUTPUT**

fun(a) fun(c) fun(b,1) fun(1,1)


The line

fun fun ('a'), fun 'b', fun ('c');

parses as

fun(fun('a'), fun('b', fun('c')));

and each parameter list is evaluated from left to right, hence the order
shown by the code's output.

HTH,

Rob


Joel

2008-03-22, 8:01 am

Thank you Rob.

I am aware of that actually. To state my confusion, I wil get a better
example:

For the following code,

sub fun {
print "fun(@_) ";
}

fun 1, fun ''b" | "c", 1;

The output looks like:
fun(c,1) fun(1,1)

*but* the precedence of the operators used is as: comma operator,
list operator, bitwise string operator.
*so* the commas and list operators should be evaluated before the pipe
gets a chance.
so the right hand side of the pipe is discarded, passing only "b" to
the funciton because the list operator is evaluated before the pipe.
But this doesn't seem to be happening!

like how in code: $a =3D 1, 2;
2 is discarded because comma has lower precedence than the assignment
operator.

Thanks,
Joel

On Mar 22, 1:10=A0am, rob.di...@gmx.com (Rob Dixon) wrote:
> Joel wrote:
>
> =A0>
>
>
>
>
>
>
>
>
>
> I suspect you are forgetting that fun() itself returns the success
> status of the print() function, introducing additional 1s into the
> output. Look:
>
> =A0 =A0local $" =3D ',';
>
> =A0 =A0sub fun {
> =A0 =A0 =A0print "fun(@_) ";
> =A0 =A0}
>
> =A0 =A0fun fun ('a'), fun 'b', fun ('c');
>
> **OUTPUT**
>
> =A0 =A0fun(a) fun(c) fun(b,1) fun(1,1)
>
> The line
>
> =A0 =A0fun fun ('a'), fun 'b', fun ('c');
>
> parses as
>
> =A0 =A0fun(fun('a'), fun('b', fun('c')));
>
> and each parameter list is evaluated from left to right, hence the order
> shown by the code's output.
>
> HTH,
>
> Rob- Hide quoted text -
>
> - Show quoted text -


Chas. Owens

2008-03-22, 8:01 am

On Sat, Mar 22, 2008 at 1:39 AM, Joel <agnel.joel@gmail.com> wrote:
> Thank you Rob.
>
> I am aware of that actually. To state my confusion, I wil get a better
> example:
>
>
> For the following code,
>
> sub fun {
> print "fun(@_) ";
> }
>
> fun 1, fun ''b" | "c", 1;
>
> The output looks like:
> fun(c,1) fun(1,1)
>
> *but* the precedence of the operators used is as: comma operator,
> list operator, bitwise string operator.
> *so* the commas and list operators should be evaluated before the pipe
> gets a chance.
> so the right hand side of the pipe is discarded, passing only "b" to
> the funciton because the list operator is evaluated before the pipe.
> But this doesn't seem to be happening!

snip

Look at the order of operations again*. The precedence in that expression is:
1. terms and list operators (leftward)
2. | ^
3. , =>
4. list operators (rightward)

The highest precedence is terms and list operators (leftward) and the
lowest is list operators (rightward). List operators have a higher
precedence than everything to their left, but a lower precedence than
everything but not, and, or, and xor to their right. So, If I place
parenthesis around each item to make it unambiguous it looks like
this:

(fun((1), (fun((("b") | ("c")), (1)))));

* http://perldoc.perl.org/perlop.html...d-Associativity
left terms and list operators (leftward)
left −>
nonassoc ++ −−
right **
right ! ~ \ and unary + and −
left =~ !~
left * / % x
left + − .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp
left &
left | ^
left &&
left ||
nonassoc .. ...
right ?:
right = += −= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Rob Dixon

2008-03-22, 7:01 pm

Joel wrote:
> Thank you Rob.
>
> I am aware of that actually. To state my confusion, I wil get a better
> example:
>
> For the following code,
>
> sub fun {
> print "fun(@_) ";
> }
>
> fun 1, fun ''b" | "c", 1;
>
> The output looks like:
> fun(c,1) fun(1,1)
>
> *but* the precedence of the operators used is as: comma operator,
> list operator, bitwise string operator.
>
> *so* the commas and list operators should be evaluated before the pipe
> gets a chance.
>
> so the right hand side of the pipe is discarded, passing only "b" to
> the funciton because the list operator is evaluated before the pipe.
> But this doesn't seem to be happening!
>
> like how in code: $a = 1, 2;
> 2 is discarded because comma has lower precedence than the assignment
> operator.


(Please bottom-post your replies on this list so that extended threads
remain comprehensible. Thank you.)

No. Highest priority is the bitwise-or, then the commas after a list
operator, then the list operator, then the commas before it. So

fun(1, fun(('b' | 'c'), 1));

perldoc perlop has a list of operator priorities.

Rob

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com