Re: passing parms to subroutine
Thanks. I didnt see anything about the $$ for refering to the value in my book , but then again Im in a hurry. anyway - I get it, it works now Lori Moon, John wrote: SUN1-BATCHperl -e 'my $a=abc; Show(\$a);sub Show {my ($value)[EMAIL PROTECTED];print value=$$value\n;}' value=abc SUN1-BATCH Hope this helps ... jwm -Original Message- From: lorid [mailto:[EMAIL PROTECTED] Sent: October 29, 2003 14:31 To: perl users Subject: passing parms to subroutine Im used to subs in C or VB but this has me puzzled... I am trying to pass a scalar ref to a subroutine, When I try to use the value of the passed parm inside the sub Im getting a garbage value (has address instead of value? ) from my process_files sub,and the increment sub, but the sub that I dont declare a prototype on or use () in the func. definition It works ?? Can someone spot my error, or tell me the right way to pass a scalar to a sub and use it ? #!/usr/bin/perl -w use warnings; use strict; #prototypes sub increment(\$); sub process_files(\$); my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); #function defs sub increment(\$){ my $reference = shift; print \nThe reference:$reference\n; print \nThe reference:@_\n; $$reference++; } sub process_files(\$){ my $file_ref = shift; print \nGot the Filename: $file_ref \n; print \nGot the Filename: @_\n; } first (1,2,3); sub first #how come no () here ? , also I notice they dont create a prototype { print \nIn first the ars are @_\n; # this allows me to get at values inside the sub which is what I need. } Thanks Lori ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: passing parms to subroutine
Lori wrote: Im used to subs in C or VB but this has me puzzled... I am trying to pass a scalar ref to a subroutine, When I try to use the value of the passed parm inside the sub Im getting a garbage value (has address instead of value? ) from my process_files sub,and the increment sub, but the sub that I dont declare a prototype on or use () in the func. definition It works ?? Can someone spot my error, or tell me the right way to pass a scalar to a sub and use it ? Hi Lori. I'd forget about declaring and prototyping Perl subroutines if I were you: it's rarely necessary. #!/usr/bin/perl -w use warnings; use strict; #prototypes sub increment(\$); sub process_files(\$); These prototypes force the type of the actual parameter to be a single scalar variable. A reference to it is passed in the subroutine's parameter array. If you left them unprototyped then Perl would just do the usual thing of passing the parameter by value, but aliasing the parameter array element to the actual parameter. Read on. my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); #function defs sub increment(\$){ my $reference = shift; print \nThe reference:$reference\n; print \nThe reference:@_\n; $$reference++; } This function is right as it stands. The only problem is that you're trying to print the reference as a scalar value without dereferencing it. If you wrote: print \nThe reference: $$reference\n; then you'd get what I think you're expecting. But the most obvious way to write this is without the prototype: sub increment { print \nParameter: $_[0]\n; $_[0]++; } so that the parameter is passed by value, but altering the @_ parameter array element also alters the actual parameter. sub process_files(\$){ my $file_ref = shift; print \nGot the Filename: $file_ref \n; print \nGot the Filename: @_\n; } Same here, but since you're not modifying the parameter there's no excuse for passing by reference anyway. You could prototype it as sub process_files($) but you may as well just accept the default behaviour: sub process_files { my $file = shift; print \nGot the Filename: $file\n; } first (1,2,3); sub first # how come no () here ? , also I notice they dont create a prototype I think I've explained that above. If you declare a subroutine then the definition has to match that declaration. But there's no need to declare any subroutines at all. { print \nIn first the ars are @_\n; # this allows me to get at values inside the sub which is what I need. } I hope this helps. Rob ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: passing parms to subroutine
Title: RE: passing parms to subroutine Look for dereferencing. Section 4.8.2 in Perl in a Nutshell -Original Message- From: lorid [mailto:[EMAIL PROTECTED]] Sent: Wednesday, October 29, 2003 2:14 PM To: Moon, John Cc: perl users Subject: Re: passing parms to subroutine Thanks. I didnt see anything about the $$ for refering to the value in my book , but then again Im in a hurry. anyway - I get it, it works now Lori Moon, John wrote: SUN1-BATCHperl -e 'my $a=abc; Show(\$a);sub Show {my SUN1-BATCH($value)[EMAIL PROTECTED];print value=$$value\n;}' value=abc SUN1-BATCH Hope this helps ... jwm -Original Message- From: lorid [mailto:[EMAIL PROTECTED]] Sent: October 29, 2003 14:31 To: perl users Subject: passing parms to subroutine Im used to subs in C or VB but this has me puzzled... I am trying to pass a scalar ref to a subroutine, When I try to use the value of the passed parm inside the sub Im getting a garbage value (has address instead of value? ) from my process_files sub,and the increment sub, but the sub that I dont declare a prototype on or use () in the func. definition It works ?? Can someone spot my error, or tell me the right way to pass a scalar to a sub and use it ? #!/usr/bin/perl -w use warnings; use strict; #prototypes sub increment(\$); sub process_files(\$); my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); my $a= 5; my $pc_unzipped_file = 'C:\\TMCC\\CIT_153\\hw9\\hw9_files\\CNUTLZD.TXT'; increment($a); print $a; process_files($pc_unzipped_file); #function defs sub increment(\$){ my $reference = shift; print \nThe reference:$reference\n; print \nThe reference:@_\n; $$reference++; } sub process_files(\$){ my $file_ref = shift; print \nGot the Filename: $file_ref \n; print \nGot the Filename: @_\n; } first (1,2,3); sub first #how come no () here ? , also I notice they dont create a prototype { print \nIn first the ars are @_\n; # this allows me to get at values inside the sub which is what I need. } Thanks Lori ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs