On Jun 2, 3:54 am, [EMAIL PROTECTED] (Alma) wrote:
> Hi,
>
> I need to pass id as parameters to a subroutine
>
> testsub(35);
>
> sub testsub($)
>    {
>    my $self = shift;
>    my $id = @_;
>    print "$id";
>
> }
>
> Its printing 3

That's just a lie.  Why are you lying to us?  Why are you not running
the code you're posting?

> am i going wrong in prototype

Yes, but not for the reasons you think you are.  For one, stop using
prototypes.  They don't work the way you think they do, and they
provide a false sense of security.  And in the example you've given,
it's completely ignored anyway (since you called it before you defined
the subroutine).

Second, stop thinking that every subroutine should start with "my
$self = shift;".  That only applies to methods.  Not "normal"
subroutines.

Third, using ANY array, including @_, in a scalar context produces the
size of that array.  In your example, that size is 0, since you
already shifted 35 off of @_ and assigned it to $self.  Therefore,
your code prints 0, not 3.

That subroutine should be written:

testsub(35);

sub testsub {
   my $id = shift;
   print $id;
}

You should really have a read of:
perldoc perlsub

rather than just finding other code to copy and paste.

Paul Lalli




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to