Just why?

Just why?

am 12.08.2011 14:03:56 von TimKapHwan

Hello all!
My first question in this group =)
Can someone explain me why this work fine:

#!/usr/bin/perl -w
sub func
{
return 10;
}
print 5 * func, "\n";
The answer is 50. Good!

But then i change func and 5:
#!/usr/bin/perl -w
sub func
{
return 10;
}
print func * 5, "\n";

The output is 10 Without multiplication by 5 and "\n".

Why this happens, plz tell.
Thank you!!


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Just why?

am 12.08.2011 20:18:02 von Shawn H Corey

On 12/08/11 08:03 AM, TimKapHwan wrote:
> Hello all!
> My first question in this group =)
> Can someone explain me why this work fine:
>
> #!/usr/bin/perl -w
> sub func
> {
> return 10;
> }
> print 5 * func, "\n";
> The answer is 50. Good!
>
> But then i change func and 5:
> #!/usr/bin/perl -w
> sub func
> {
> return 10;
> }
> print func * 5, "\n";
>
> The output is 10 Without multiplication by 5 and "\n".
>
> Why this happens, plz tell.
> Thank you!!
>
>

Try: print func() * 5, "\n";

The above means: print func( *5, "\n" );

where *5 is a reference to the typeglob 5. See `perldoc perlreftut` and
`perldoc perlref` for details.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Just why?

am 12.08.2011 20:22:07 von Robert Wohlfarth

--20cf30427190bc86c504aa52fdcb
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Aug 12, 2011 at 7:03 AM, TimKapHwan wrote:

> #!/usr/bin/perl -w
> sub func
> {
> return 10;
> }
> print func * 5, "\n";
>

Perl interprets that command like this:
print func(*5, "\n");

The "*5" and "\n" become parameters to func. Change the code like this:
print func() * 5, "\n";

--
Robert Wohlfarth

--20cf30427190bc86c504aa52fdcb--

Re: Just why?

am 12.08.2011 20:29:41 von Shawn H Corey

On 12/08/11 02:22 PM, Robert Wohlfarth wrote:
> On Fri, Aug 12, 2011 at 7:03 AM, TimKapHwan wrote:
>
>> #!/usr/bin/perl -w
>> sub func
>> {
>> return 10;
>> }
>> print func * 5, "\n";
>>
>
> Perl interprets that command like this:
> print func(*5, "\n");
>
> The "*5" and "\n" become parameters to func. Change the code like this:
> print func() * 5, "\n";
>

Or you could change the function like this:

sub func() {
return 10;
}

This is the one time where prototypes can be used without messing things
up quickly. :)

See `perldoc perlsub` and search for the section on "Prototypes".


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Just why?

am 12.08.2011 22:47:42 von Olga Lee

Oh, thank you!!
Now I understand, the point is that if don't suppose to give an
argument to function, i should declare it as function() with brackets.

Thanks again!


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Just why?

am 13.08.2011 04:28:36 von Uri Guttman

>>>>> "OL" == Olga Lee writes:

OL> Oh, thank you!!
OL> Now I understand, the point is that if don't suppose to give an
OL> argument to function, i should declare it as function() with brackets.

that is actually the wrong point. don't use prototypes unless there is a
major benefit to using them (and there are a few). the correct thing to
take from this is to always call your functions with (). that is good in
several ways. it means you don't ever have to predeclare them, it means
you will always see and know what is being passed to the call and it
won't ever be parsed incorrectly.

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Just why?

am 14.08.2011 06:39:36 von Rob Dixon

On 12/08/2011 19:22, Robert Wohlfarth wrote:
> On Fri, Aug 12, 2011 at 7:03 AM, TimKapHwan wrote:
>
>> #!/usr/bin/perl -w
>> sub func
>> {
>> return 10;
>> }
>> print func * 5, "\n";
>>
>
> Perl interprets that command like this:
> print func(*5, "\n");
>
> The "*5" and "\n" become parameters to func.

Not quite. func is being treated as a filehandle, and *5 and "\n" are
parameters to print. If warnings were enabled you should get a message
to the effect that filehandle func is not open.

Try running

print STDOUT * 5, "\n";

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/