>     I'm sorry about that. I didn't mean to flame
> you. It just
> seemed like you were taking the long way around to
> solve the
> problem.
> 

It was a year ago.  I don't remember who it was, and
it's not a big deal anymore.

> 
>     That's funny. When I started reading the
> documentation a few
> years ago, I got the distinct impression that only
> programmers
> with a background in C were being invited to the
> perl party. I
> even complained about it on the another beginner
> email list.
> 

My background in C extends to pointers -- I'm still
the novice there. :)

<snip>

>     Okay. In perl everything is passed into the sub
> with a special
> array called @_. Let's call a subroutine named
> link(). (I'm going
> to use list and array interchangeably here. They're
> not really the
> same though.)
> 
> print link( '/perl/', 'Learning Perl' );
> 
>     Behind the scenes, perl creates a list of the
> two arguments
> '/perl/' and 'Learning Perl' and sets it equal to
> @_. It then
> passes control to link().
> 

Before we go on, what if you had $a='/perl/'; $b =
'Learning Perl';
then did:
print link($a, $b);  <-that's how I'd pass
information, as variables, not literals.
Does this change what comes next?

>     Inside link() we need to access @_ to get to the
> arguments.
> What is declared on the subroutine declaration line
> is not use by
> perl to transfer subroutine arguments. It is only
> used for type
> checking.
> 
>     Let's write link():
> 
> sub link {
>     my( $href, $name ) = @_;
>     return qq(<a href="$href">$name</a>);
> }
> 
>     It is pretty obvious how the we accessed @_
> here, but in
> ParseLineForHomeAndVisitors() we have this:
> 
> sub ParseLineForHomeAndVisitors($line) {
>     my $line  = shift;
>     my @teams = ( $line =~ /([[:alpha:]]+)/g );
>     return @teams;
> }
> 
>     The second line is accessing @_. 'shift'
> operates on @_ by
> default, so
> 

This is all making sense, sort of like when shift
operates on $_.

>     my $line  = shift;
> 
>     is shorthand for:
> 
>     my $line  = shift @_;
> 
> 
>     In perl, declaring subs at the top of the script
> is necessary
> when using prototypes. 

It's not necessary otherwise?  Like:
#sub declarations
sub ParseLineForHomeAndVisitors;
sub link;

That's how I do it, whether or not there is something
I want to pass or return with the sub.

>Prototypes in perl do not
> work the way you
> are using them. They do not declare a variable. They
> restrain the
> type of values which can be passed to a subroutine.
> 

So when Cozens uses something like:
sub link($$);
if he were passing arrays, he'd do:
sub link(@@);
and hashes:
sub link(%%);
Is that correct?  That's what it seems you are saying.

>     Perl prototyping handles this type checking for
> the
> programmer, but it still uses @_ to pass arguments
> for the sub. I
> have always found prototypes too restrictive. I
> understand why you
> are using them, but I still think life will be
> easier if you
> abandon them.
> 

Restrictive, but protective.  I find perl to allow too
much freedom.  And, as a novice programmer, I'd rather
be as clear as day.  But I see your point.

<snip>

>     I agree. However, that shouldn't be the goal (in
> perl) for a
> beginner. It is possible, but it requires the
> knowledge you'll
> obtain by using some data structures.
> 

I'm not sure what you're saying here.

> 
> : And then below, I'd have a section called sub
> definitions, where
> : I'd define all those subs, and maybe others that
> made up those
> : subs.  Writing like this is easier on my eyes and
> tells me and
> : someone else (given my subs have descriptive
> names) a very good
> : idea of what I want to do.  I write subs for
> printing out a few
> : lines of intro instructions.  It just makes sense
> to me.  So
> : that's why a sub.
> 
>     In perl, one problem with using a sub this way
> is that it
> forces the use of global variables. If
> %AbbrevAndNicknames has not
> been declared above the subroutine, how else will
> you pass it
> around the script?
> 

Hmm, not sure.  I used to be able to do it in C,
without globals, but I haven't gotten that far in
perl.

<snip>

 
>     You might want to depend on the documentation
> that comes with
> perl more than the book you are using. 'perlfunc'
> has a listing of
> almost all the the functions you'll see here.
> Perldoc.com has
> handy html documentation for the major versions of
> perl. 'Perlsub'
> does a better, deeper job than me with subroutines.
> 

Yeah, I had this problem before.  Someone told me to
go to STart->Program Files->ActiveSTate 5.8->
Documentation
This gave me what I find at the manpages, not perldoc.
 It was laid out well, but it was confusing.


>     Here is a small script to show typical
> processing of a file in
> perl. Instead of opening a file. I'll use the
> special file handle
> DATA, which is everything past the __END__ tag in
> this script.
> Hopefully, it will give you a basis for writing subs
> in perl.
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use Data::Dumper 'Dumper';
> 
> # @teams is at file level scope. Perl allows
> # it to be used anywhere in this file (script).
> # We assume responsibility not to use it as a
> # global variable.
> my @teams;
> 
> # $line is scoped within this while block
> # It disappears after the brace ends the block.
> while ( defined( my $line = <DATA> ) ) {
> 
>     # This goes to the next line unless we get
>     # a match for teams.
>     next unless $line =~
> /^[[:alpha:]]+\s+[[:digit:]]+,/;
> 
>     # We have a match, let's fetch the teams
>     @teams = fetch_teams( $line );
> 
>     # Stop processing the file
>     last;
> }
> 
> # Dumper() provides an easy method to see
> # the composition of even complex data structures
> print Dumper [EMAIL PROTECTED];
> 
> # At this point <DATA> will point to the third
> # line '1st Period'
> 
> while ( defined( my $line = <DATA> ) ) {
>     #
>     # process the stats
>     #
> }
> 
> sub fetch_teams {
>     my $line = shift;
>     return ( $line =~ /([[:alpha:]]+)/g );
> }
> __END__
> 
> Spurs 94, Suns 82
> 1st Period
> (12:00) Jump Ball Robinson vs Williams
> (11:41) [PHX] Marion Turnaround Jump: Missed
> 
> ------------------

I understand this one, but not the one below.
I'm not sure what is supposed to be in @_.  And I'm a
bit confused why you're using a reference.

little help?

__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com

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


Reply via email to