Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > books on perl

Reply
Thread Tools

books on perl

 
 
K4 Monk
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 10:38*pm, "J. Gleixner" <glex_no-s...@qwest-spam-
no.invalid> wrote:
> K4 Monk wrote:
> > I've installed it now. I've programmed (if you can call it that) in
> > perl for years and never heard about perldoc. I generally wrote
> > whatever scripts I had to write with a bit of fiddling around and
> > looking at existing code

>
> Welcome to the 21st century.
>
> Be sure to learn about CPAN too: *http://www.cpan.org/


Thank you! btw, I posted this in another newsgroup but never got a
response. After reading this thread and one of the booke (HOP) I have
realized that I don't know Perl. I've never used functions extensively
and don't understand how they work. And here's a program I wrote to
prove it.

#!/usr/bin/perl
use strict;

sub func {
my %list;
$list{"map"} = "key";
$list{"l"}="j";

my @arr;
push (@arr, "egg");
push (@arr, "hell");


return (%list, @arr);

}

my @arrref = &func();
my %l = %{$arrref[0]};
my @r = @{$arrref[1]};

print "keys\n";
foreach my $k(keys %l) { print "$k\n"; }
print "array\n";
foreach my $rr(@r) { print "$rr\n"; }


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-23-2011
K4 Monk <(E-Mail Removed)> wrote:
>Thank you! btw, I posted this in another newsgroup but never got a
>response. After reading this thread and one of the booke (HOP) I have
>realized that I don't know Perl. I've never used functions extensively
>and don't understand how they work.


See "perldoc perlsub"

>And here's a program I wrote to
>prove it.
>
>#!/usr/bin/perl
>use strict;


Good. But you should also enable warnings.

use warnings;

>sub func {
> my %list;
> $list{"map"} = "key";
> $list{"l"}="j";


You can write such an initialization more easily as
my %list = (
"map" => "key",
"l" => "j");

> my @arr;
> push (@arr, "egg");
> push (@arr, "hell");


Most people would probably do a simple
my @arr = ('egg", "hell");

> return (%list, @arr);


You are aware that you are returning a list with 6 elements, mixing your
hash and array elements indiscrimently togehter, aren't you?
Just like arguments the return value of a sub is just a list of scalars,
too, and any sub-structure or composite data will be flattened.

>}
>
> my @arrref = &func();


Just print the lenght of the array here
print scalar(@arrref);
and it will tell you that @arrref contains 6 elements.

> my %l = %{$arrref[0]};
> my @r = @{$arrref[1]};


Whatever you are trying to do here doesn't work because @arrref already
contains the wrong data. If you want to preserve your return hash and
return array from sub func then first of all you have to return a
reference to them instead of their values (see "Make Rule 1" in "Making
References" in "perldoc perlreftut")

return (\%list, \@arr);

Then the rest will more or less fall into place on its own.

jue
 
Reply With Quote
 
 
 
 
K4 Monk
Guest
Posts: n/a
 
      02-23-2011
On Feb 23, 8:09*am, J rgen Exner <(E-Mail Removed)> wrote:
> K4 Monk <(E-Mail Removed)> wrote:
> >Thank you! btw, I posted this in another newsgroup but never got a
> >response. After reading this thread and one of the booke (HOP) I have
> >realized that I don't know Perl. I've never used functions extensively
> >and don't understand how they work.

>
> See "perldoc perlsub"
>
> >And here's a program I wrote to
> >prove it.

>
> >#!/usr/bin/perl
> >use strict;

>
> Good. But you should also enable warnings.
>
> * * * * use warnings;
>
> >sub func {
> > * *my %list;
> > * *$list{"map"} = "key";
> > * *$list{"l"}="j";

>
> You can write such an initialization more easily as
> * * * * my %list = (
> * * * * * * "map" => "key",
> * * * * * * "l" => "j");
>
> > * *my @arr;
> > * *push (@arr, "egg");
> > * *push (@arr, "hell");

>
> Most people would probably do a simple
> * * * * my @arr = ('egg", "hell");
>
> > * *return (%list, @arr);

>
> You are aware that you are returning a list with 6 elements, mixing your
> hash and array elements indiscrimently togehter, aren't you?


Thank you jue, no I wasn't aware of this, and on my end I spent an
hour looking for the but gave up.

> Just like arguments the return value of a sub is just a list of scalars,
> too, *and any sub-structure or composite data will be flattened.


so, even if I do a func(%hash), on func's end it gets an array of a
scalar pointing to %hash?


>
> >}

>
> > my @arrref = &func();

>
> Just print the lenght of the array here
> * * * * print scalar(@arrref);


nice. I noted this down.

> and it will tell you that @arrref contains 6 elements.
>
> > my %l = %{$arrref[0]};
> > my @r = @{$arrref[1]};

>
> Whatever you are trying to do here doesn't work because @arrref already
> contains the wrong data. If you want to preserve your return hash and
> return array from sub func then first of all you have to return a
> reference to them instead of their values (see "Make Rule 1" in "Making
> References" in "perldoc perlreftut")
>
> * * * * return (\%list, \@arr);
>
> Then the rest will more or less fall into place on its own.
>
> jue


Thanks jue for your help!
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-24-2011
K4 Monk <(E-Mail Removed)> wrote:
>so, even if I do a func(%hash), on func's end it gets an array of a
>scalar pointing to %hash?


No. It gets a flat(!) list(!) which is composed of the alternating keys
and values of %hash, i.e. if %hash has 5 entries then the argument list
@_ of func(%hash) will contain 10 values.

jue
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      02-24-2011
On 2011-02-23, K4 Monk <(E-Mail Removed)> wrote:
>
> so, even if I do a func(%hash), on func's end it gets an array of a
> scalar pointing to %hash?


AIUI (and ICBW, in which case this'll be a learning exercise for me
too), if you do func(%hash) what func receives *is* an array, there's
no 'pointing to'. For example:

my %hash = (
john => "lennon",
paul => "macca",
george => "harrison",
ringo => "star",
);
func(%hash);

func() would receive an array containing qw/john, lennon, paul, macca,
george, harrison, ringo, star/ ... which can be shown by:
sub func {
print $_, "\n" for @_;
}

You could massage the array back into a hash in func() with (for
example)
sub func {
my %hash = @_;
# do stuff ...
}

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
K4 Monk
Guest
Posts: n/a
 
      02-24-2011
On Feb 24, 5:03*am, Tad McClellan <(E-Mail Removed)> wrote:
> K4 Monk <(E-Mail Removed)> wrote:
> > On Feb 23, 8:09*am, J rgen Exner <(E-Mail Removed)> wrote:
> >> K4 Monk <(E-Mail Removed)> wrote:
> >> >I've never used functions extensively
> >> >and don't understand how they work.

>
> >> See "perldoc perlsub"

>
> Have you done that yet?


yep, finally!

"The Perl model for function call and return values is simple:
all functions are passed as
parameters one single flat list of scalars, and all functions
likewise return to their caller one
single flat list of scalars. Any arrays or hashes in these
call and return lists will collapse,
losing their identities--but you may always use pass-by-
reference instead to avoid this. "
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      02-24-2011
On 2011-02-24, Tad McClellan <(E-Mail Removed)> wrote:
> Justin C <(E-Mail Removed)> wrote:
>> On 2011-02-23, K4 Monk <(E-Mail Removed)> wrote:
>>>
>>> so, even if I do a func(%hash), on func's end it gets an array of a
>>> scalar pointing to %hash?

>>
>> AIUI (and ICBW, in which case this'll be a learning exercise for me
>> too), if you do func(%hash) what func receives *is* an array,

>
>
> Not it *is not* an array.
>
> It is a list.


kick($self)

I know this, why do I always call it an array?! <shakes head>

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      02-24-2011
On Thu, 24 Feb 2011 14:18:32 +0000, Justin C wrote:

> kick($self)


Better written as:

$self->kick();

although if kick() does not exist, the former fails at compile time while
the latter fails at run time.



M4
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      02-24-2011
>>>>> "Martijn" == Martijn Lievaart <(E-Mail Removed)> writes:

Martijn> Better written as:

Martijn> $self->kick();

Martijn> although if kick() does not exist, the former fails at compile time while
Martijn> the latter fails at run time.

And if kick doesn't know what to do with an object of the type that
$self is, it'll have to check that itself unless it's a method call.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      02-25-2011
On 2011-02-25, Tad McClellan <(E-Mail Removed)> wrote:
> Justin C <(E-Mail Removed)> wrote:
>> On 2011-02-24, Tad McClellan <(E-Mail Removed)> wrote:
>>> Justin C <(E-Mail Removed)> wrote:
>>>> On 2011-02-23, K4 Monk <(E-Mail Removed)> wrote:
>>>>>
>>>>> so, even if I do a func(%hash), on func's end it gets an array of a
>>>>> scalar pointing to %hash?
>>>>
>>>> AIUI (and ICBW, in which case this'll be a learning exercise for me
>>>> too), if you do func(%hash) what func receives *is* an array,
>>>
>>>
>>> Not it *is not* an array.
>>>
>>> It is a list.

>>
>> kick($self)
>>
>> I know this, why do I always call it an array?! <shakes head>

>
>
> It even happens to Larry.
>
> He gave us wantarray() rather than wantlist()


I'm in good company then! That doesn't make me feel so bad.

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design Patterns Books and Best Practices Books. Luis Esteban Valencia ASP .Net 4 07-01-2005 07:29 AM
Books, books, books: best reference texts for Verilog and VHDL HDL Book Seller VHDL 0 12-01-2004 02:26 AM
Books, Books, Books... C++ 3 09-19-2004 10:11 PM
exchange MCSD books for security books MCSD Frank MCSE 0 01-12-2004 12:17 AM
Finished Courses, Selecting Books for Exam Preparation (List of Books) Mac MCSD 2 12-17-2003 01:48 PM



Advertisments