[32880] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4158 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 1 05:17:30 2014

Date: Sat, 1 Mar 2014 02:17:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 1 Mar 2014     Volume: 11 Number: 4158

Today's topics:
        grep $re gotcha <john@castleamber.com>
    Re: grep $re gotcha <ben@morrow.me.uk>
    Re: grep $re gotcha <john@castleamber.com>
    Re: use strict; use warnings; <triflemenot@protocol.invalid>
    Re: using a library <mach2@hushmail.com>
    Re: using a library <john@castleamber.com>
    Re: using a library <mach2@hushmail.com>
    Re: using a library <ben@morrow.me.uk>
    Re: using a library <ben@morrow.me.uk>
    Re: using a library <john@castleamber.com>
    Re: using a library <mach2@hushmail.com>
    Re: using a library <hjp-usenet3@hjp.at>
    Re: using a library <janek_schleicher@yahoo.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 28 Feb 2014 17:14:23 -0600
From: John Bokma <john@castleamber.com>
Subject: grep $re gotcha
Message-Id: <878usuygmo.fsf@castleamber.com>


Maybe not a gotcha for everybody, but I bumped into this today:

perl -e '
my $re = qr/[aeiou]/;
print join( ", ", grep $re, "a".."z" ), "\n";
print join( ", ", grep /$re/, "a".."z" ), "\n";
'
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
a, e, i, o, u

I was expecting grep $re to be short for grep /$re/.

-- 
John Bokma                                                               j3b

Blog: http://johnbokma.com/        Perl Consultancy: http://castleamber.com/
Perl for books:    http://johnbokma.com/perl/help-in-exchange-for-books.html


------------------------------

Date: Sat, 1 Mar 2014 01:13:41 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: grep $re gotcha
Message-Id: <5ro7ua-h4q1.ln1@anubis.morrow.me.uk>


Quoth John Bokma <john@castleamber.com>:
> 
> Maybe not a gotcha for everybody, but I bumped into this today:
> 
> perl -e '
> my $re = qr/[aeiou]/;
> print join( ", ", grep $re, "a".."z" ), "\n";
> print join( ", ", grep /$re/, "a".."z" ), "\n";
> '
> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
> a, e, i, o, u
> 
> I was expecting grep $re to be short for grep /$re/.

perldoc -f grep? The first argument to grep is an expression; a qr// is
a reference, so it's always true. split is the only builtin which
implicitly treats its argument as a regex.

Ben



------------------------------

Date: Fri, 28 Feb 2014 19:26:17 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: grep $re gotcha
Message-Id: <87zjlawvye.fsf@castleamber.com>

Ben Morrow <ben@morrow.me.uk> writes:

> Quoth John Bokma <john@castleamber.com>:
>> 
>> Maybe not a gotcha for everybody, but I bumped into this today:
>> 
>> perl -e '
>> my $re = qr/[aeiou]/;
>> print join( ", ", grep $re, "a".."z" ), "\n";
>> print join( ", ", grep /$re/, "a".."z" ), "\n";
>> '
>> a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
>> a, e, i, o, u
>> 
>> I was expecting grep $re to be short for grep /$re/.
>
> perldoc -f grep? The first argument to grep is an expression; a qr// is
> a reference, so it's always true. split is the only builtin which
> implicitly treats its argument as a regex.

Yes, I saw immediately what went wrong. Never done this before, but when
I ran it (different code) I was like: huh? why do I get also results
that shouldn't be there.

-- 
John Bokma                                                               j3b

Blog: http://johnbokma.com/        Perl Consultancy: http://castleamber.com/
Perl for books:    http://johnbokma.com/perl/help-in-exchange-for-books.html


------------------------------

Date: Fri, 28 Feb 2014 23:34:12 +0000
From: Trifle Menot <triflemenot@protocol.invalid>
Subject: Re: use strict; use warnings;
Message-Id: <iu62h9hjsnrfiitfgsojjf2cb831ilacpu@4ax.com>

On Fri, 28 Feb 2014 21:54:24 +0000, Rainer Weikusat
<rweikusat@mobileactivedefense.com> wrote:

>John Black <johnblack@nospam.com> writes:

>> Its location 50, so its also the 50th location.  Why make it
>> confusing?

>I suggest that you ask a few people without 'computing background' how
>many 'locations' they expect to be in front of "the 50th location" and
>then reconsider your idea of what is and isn't confusing ...

I wonder why programmers make so many off by one errors. But maybe it's
just bad programmers who have little aptitude for the job.




------------------------------

Date: Fri, 28 Feb 2014 18:03:19 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: using a library
Message-Id: <Ze2dnS-cLfhavozOnZ2dnUVZ_qGdnZ2d@supernews.com>

On Fri, 28 Feb 2014 22:48:54 +0100, Peter J. Holzer wrote:

>> 	my $input = shift(@_);
> 
> This takes the first element of @_ and shoves it into $input. What is
> @_? It's an array holding the parameters with which the function was
> called. So instead of using just the first one, we use them all:
> 
> sub getdivby2 {
>     my @numbers = @_;
>     for (@numbers) {
>         $_ /= 2;
>     }
>     return @numbers;
> }
> 
> And as you can see here, I just return an array as well. Perl functions
> can return lists, not just scalar values. (there is also a difference
> between lists and arrays, which we like to discuss at great length, but
> for now you can just assume that these are synonyms).
> 
> And then you can call the function the way you would expect:
> 
> my @numbers =split($input);
> my @div = getdivby2(@numbers);
> say "@div";
> 
> @div = getdivby2(2, 3, 4);
> say "@div";
> 
> @numbers = (2, 3);
> 
> @div = getdivby2(1, @numbers, 4);
> say "@div";
> 
> (the last example is probably why Charlton wrote that passing a
> reference to an array is safer).
> 
>         hp

As you say, was much easier than I thought. All I wanted to do was copy 
the @_ but I must have been doing something else. When I tried again with 
my @array = (@_); I got the array. That's basically all I needed to know. 

Wish I could remember what I tried and failed with, but it ended up being 
really simple. 

Thanks!
-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Fri, 28 Feb 2014 18:11:10 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: using a library
Message-Id: <874n3iye01.fsf@castleamber.com>

Marek Novotny <mach2@hushmail.com> writes:

> Wish I could remember what I tried and failed with, but it ended up being 
> really simple. 

If you're eager to learn new stuff check out git. With git you can keep
versions around that failed, etc.

-- 
John Bokma                                                               j3b

Blog: http://johnbokma.com/        Perl Consultancy: http://castleamber.com/
Perl for books:    http://johnbokma.com/perl/help-in-exchange-for-books.html


------------------------------

Date: Fri, 28 Feb 2014 18:29:01 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: using a library
Message-Id: <Ze2dnSmcLfhQtIzOnZ2dnUVZ_qGdnZ2d@supernews.com>

On Fri, 28 Feb 2014 18:11:10 -0600, John Bokma wrote:

> Marek Novotny <mach2@hushmail.com> writes:
> 
>> Wish I could remember what I tried and failed with, but it ended up
>> being really simple.
> 
> If you're eager to learn new stuff check out git. With git you can keep
> versions around that failed, etc.

Just trying to finish this course so I can start the actual dedicated 
Perl course. 90% done now. 30 objectives down, 3 to go. I had to move the 
card shuffling to a library. 

Once done with an objective it looks so dumb and easy. :)

-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 1 Mar 2014 01:15:07 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using a library
Message-Id: <rto7ua-h4q1.ln1@anubis.morrow.me.uk>


Quoth Marek Novotny <mach2@hushmail.com>:
> 
> As you say, was much easier than I thought. All I wanted to do was copy 
> the @_ but I must have been doing something else. When I tried again with 
> my @array = (@_); I got the array. That's basically all I needed to know. 
> 
> Wish I could remember what I tried and failed with, but it ended up being 
> really simple. 

Probably you had

    my @array = shift(@_);

Ben



------------------------------

Date: Sat, 1 Mar 2014 01:10:10 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: using a library
Message-Id: <iko7ua-h4q1.ln1@anubis.morrow.me.uk>


Quoth Marek Novotny <mach2@hushmail.com>:
> 
> #!/usr/bin/perl
> # File: obj13-1.pl
> # 
> # The script file, obj13-1.pl should get a list of numbers from the user
> # (either via STDIN or a list of arguments), call the library function and
> # print the returned numbers. Be sure the program gives instructions to
> # the user of how to input the numbers and, once the numbers are entered,
> # how to complete the program.
>
> use strict;
> use warnings;

*Good*.

> 
> require 'obj13-lib.pl';

Presumably this style of library is the one you have been told to use?
It would be considered better practice these days (by which I mean,
since 1994, when Perl 5 was released) to call your library something
like Marek/Obj13.pm and load it like

    require Marek::Obj13;

You would also use a 'package' statement inside that file to put the
functions into a different namespace (this means, you can have two
libraries in the same program which both define a function called 'foo',
and they don't get mixed up, and you can choose which one you want to
call).

However it may just be that your instructors are starting with Perl-4-
style libraries because they're a little simpler.

> print "\nPlease enter a few numbers then hit <RETURN>: ";
> my $input = <STDIN>;

'Reading the question carefully' is an important part of any assignment.
This program is supposed to also accept a list of numbers on the command
line. (I'm not going to tell you how to do that.)

> my $average = getaverage($input);
> my $total = gettotal($input);
> my $div = getdivby2($input);

Good. This is the right way to use 'my'.

> 
> print "\n";
> print "You entered: $input\n"; 
> print "The average of your numbers is: $average\n";
> print "The total of all your numbers combined is: $total\n";
> print "Each of your numbers divided by 2 are: $div\n\n";
> print "Have a nice day!!\n\n";

A great long sequence of print statements is, I'm afraid, one of the
classic signs of a Perl beginner :). Perl has a quoting construction
called a 'here document' that is rather clearer; this was inherited from
Bourne shell, so if you've learned that you might be familiar with it
already:

    print <<OUTPUT;

    You entered: $input
    The average of your numbers is: $average
    The total of all your numbers combined is: $total
    Your numbers each divided by 2 are: $div

    Have a nice day!

    OUTPUT

(Note that I've indented the code in this post for clarity. In a real
program, the final OUTPUT marker must appear at the left margin,
regardless of the indentation of the surrounding code.)

> 
> --end---------------------------------
> 
> And it makes use of this library:
> 
> --begin------------------------------------------
> 
> #!/usr/bin/perl
> # File: obj13-lib.pl
> # 
> # The library should contain a function that takes in an array of numbers
> # (of arbitrary size). The function will then calculate the average of the
> # numbers, the total of all of the numbers added together, and a new array
> # of numbers which is comprised of the original input numbers each divided
> # by 2. It will then return a new list with all of that information.

This library is supposed to contain *one* function, that accepts an
array of numbers, and returns a single list with all the output
information. This is not a terribly clear spec; I would assume that they
mean the function to take a list of arguments (rather than an arrayref,
as some other posters have assumed), and is to return a list consisting
of the average, the total, and the numbers-divided-by-2 in that order. 

This is not quite what you've written. What you've done would be
(mostly) satisfactory in a real program that needed to calculate those
quantities, but for an assignment it's worth doing exactly what's been
asked for.

> use strict;
> use warnings;
> 
> sub getdivby2{
> 	my $input = shift(@_);

I may get some argument from the other regulars here, but except in
special circumstances I don't like using shift to pull arguments off @_.
IMHO it's much clearer to put all the arguments you want in named
variables with one list assignment, like this:

    my ($input) = @_;

> 	chomp($input);
> 	my @numbers = split(/ /,$input);

These two steps should have been done by the code outside the function.
(The function is supposed to accept a list of numbers, not a
space-separated string.) In the case of getting numbers from the
command line, you won't need to do it at all, since the shell has
already done it for you.

> 	my @divided = @numbers;
> 	my $elem;	
> 	foreach $elem(@divided){
> 		$elem/=2;
> 		}

Hey, that's good! Getting the hang of the aliasing behaviour of
for(each) is one of the less trivial early steps in learning Perl.

It is worth pointing out that if you'd written this instead:

    foreach my $elem (@divided) {

the $elem variable would only exist inside the loop, not afterwards. As
ever, limiting the scope of a variable is always a good idea when you
can, since it reduces the risk of some other code picking it up
unexpectedly.

> 	my $divided = join(" ",@divided);
> 	return $divided;
> }
> 
> sub getaverage{
> 	my $input = shift(@_);
> 	chomp($input);
> 	my @numbers = split(/ /,$input);
> 	my $qty = $#numbers +1;

    my $qty = @numbers;

> 	my $combined;	
> 	$combined += $_ for @numbers;
> 	my $average = ($combined / $qty);

or indeed simply

    my $average = ($combined / @numbers);

Using an array where a scalar value is expected ('in scalar context')
gives you the length of the array. This is logically cleaner, as well as
simpler, than adding one to the index of the last element.

[In the old days it was safer as well, because Perl had a way to change
arrays from being 0-based to being anything-else-based (typically 1).
This 'feature' was so confusing it was eventually (in 5.16) removed.]

> 	return $average;
> }
> 
> sub gettotal{
> 	my $input = shift(@_);
> 	chomp($input);
> 	my @numbers = split(/ /,$input);
> 	my $combined;	
> 	$combined += $_ for @numbers;

Be consistent about whether you write 'for' or 'foreach'. They are
exactly equivalent, so most people choose to just use 'for', because
it's shorter.

> 	return $combined;
> }

Now, here is an exercise in thinking like a programmer. Look at this
function. Look at the previous function. They seem to be very similar,
don't they? What could you do about that?

> My question is this. In the above example I take an array and convert it 
> to a string where it is passed to the library where the string is 
> converted back to an array. Then I process my answer, which is converted 
> to a string and returned back to the Perl script. 
> 
> Can I simply pass an array without conversion to a string??

You are already, in fact, passing a list, which comes into the function
as an array; you just happen to have passed a list of only one element.
@_, despite it's rather odd appearance, is actually just a (mostly)
perfectly ordinary array variable. So:

    sub foo {
        for (@_) {
            print "$_\n";
        }
    }

    foo 1, 2, 3;

    my @array = (4, 5, 6);
    foo @array;

    foo 3, @array, 7;

Ben



------------------------------

Date: Fri, 28 Feb 2014 19:29:18 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: using a library
Message-Id: <87vbvywvtd.fsf@castleamber.com>

Marek Novotny <mach2@hushmail.com> writes:

> On Fri, 28 Feb 2014 18:11:10 -0600, John Bokma wrote:
>
>> Marek Novotny <mach2@hushmail.com> writes:
>> 
>>> Wish I could remember what I tried and failed with, but it ended up
>>> being really simple.
>> 
>> If you're eager to learn new stuff check out git. With git you can keep
>> versions around that failed, etc.
>
> Just trying to finish this course so I can start the actual dedicated 
> Perl course. 90% done now. 30 objectives down, 3 to go. I had to move the 
> card shuffling to a library. 

Still, recommendation stays: learn to use git (=version control) early
on. In a few weeks or so you will thank me ;-)

Ditto for testing; Test:: modules on CPAN, I use Test::Most a lot. You
might want to read the documentation of Test::Simple and Test::More for
starters. Especially if you start to write libraries / modules.

-- 
John Bokma                                                               j3b

Blog: http://johnbokma.com/        Perl Consultancy: http://castleamber.com/
Perl for books:    http://johnbokma.com/perl/help-in-exchange-for-books.html


------------------------------

Date: Fri, 28 Feb 2014 20:50:05 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: using a library
Message-Id: <Ze2dnSicLfhA14zOnZ2dnUVZ_qGdnZ2d@supernews.com>

On Sat, 01 Mar 2014 01:10:10 +0000, Ben Morrow wrote:

> Quoth Marek Novotny <mach2@hushmail.com>:
>> 
>> #!/usr/bin/perl # File: obj13-1.pl #
>> # The script file, obj13-1.pl should get a list of numbers from the
>> user # (either via STDIN or a list of arguments), call the library
>> function and # print the returned numbers. Be sure the program gives
>> instructions to # the user of how to input the numbers and, once the
>> numbers are entered, # how to complete the program.
>>
>> use strict;
>> use warnings;
> 
> *Good*.
> 
> 
>> require 'obj13-lib.pl';
> 
> Presumably this style of library is the one you have been told to use?
> It would be considered better practice these days (by which I mean,
> since 1994, when Perl 5 was released) to call your library something
> like Marek/Obj13.pm and load it like
> 
>     require Marek::Obj13;
> 
> You would also use a 'package' statement inside that file to put the
> functions into a different namespace (this means, you can have two
> libraries in the same program which both define a function called 'foo',
> and they don't get mixed up, and you can choose which one you want to
> call).
> 
> However it may just be that your instructors are starting with Perl-4-
> style libraries because they're a little simpler.
> 
>> print "\nPlease enter a few numbers then hit <RETURN>: ";
>> my $input = <STDIN>;
> 
> 'Reading the question carefully' is an important part of any assignment.
> This program is supposed to also accept a list of numbers on the command
> line. (I'm not going to tell you how to do that.)
> 
>> my $average = getaverage($input);
>> my $total = gettotal($input);
>> my $div = getdivby2($input);
> 
> Good. This is the right way to use 'my'.
> 
> 
>> print "\n";
>> print "You entered: $input\n";
>> print "The average of your numbers is: $average\n";
>> print "The total of all your numbers combined is: $total\n";
>> print "Each of your numbers divided by 2 are: $div\n\n";
>> print "Have a nice day!!\n\n";
> 
> A great long sequence of print statements is, I'm afraid, one of the
> classic signs of a Perl beginner :). Perl has a quoting construction
> called a 'here document' that is rather clearer; this was inherited from
> Bourne shell, so if you've learned that you might be familiar with it
> already:
> 
>     print <<OUTPUT;
> 
>     You entered: $input The average of your numbers is: $average The
>     total of all your numbers combined is: $total Your numbers each
>     divided by 2 are: $div
> 
>     Have a nice day!
> 
>     OUTPUT

This is awesome. I just wrote a sample script to test it out. That's 
cool! They made zero mention of this by the way. 

> (Note that I've indented the code in this post for clarity. In a real
> program, the final OUTPUT marker must appear at the left margin,
> regardless of the indentation of the surrounding code.)
> 
> 
>> --end---------------------------------
>> 
>> And it makes use of this library:
>> 
>> --begin------------------------------------------
>> 
>> #!/usr/bin/perl # File: obj13-lib.pl #
>> # The library should contain a function that takes in an array of
>> numbers # (of arbitrary size). The function will then calculate the
>> average of the # numbers, the total of all of the numbers added
>> together, and a new array # of numbers which is comprised of the
>> original input numbers each divided # by 2. It will then return a new
>> list with all of that information.
> 
> This library is supposed to contain *one* function, that accepts an
> array of numbers, and returns a single list with all the output
> information. This is not a terribly clear spec; I would assume that they
> mean the function to take a list of arguments (rather than an arrayref,
> as some other posters have assumed), and is to return a list consisting
> of the average, the total, and the numbers-divided-by-2 in that order.
> 
> This is not quite what you've written. What you've done would be
> (mostly) satisfactory in a real program that needed to calculate those
> quantities, but for an assignment it's worth doing exactly what's been
> asked for.

Believe it or not, I read it that same way, wrote a nice script that did 
everything they asked for in one shot, and they rejected it. They wanted 
it split up. It's cool, I ended up liking the 2nd version more anyway. It 
is cleaner than my first for sure. 

>> use strict;
>> use warnings;
>> 
>> sub getdivby2{
>> 	my $input = shift(@_);
> 
> I may get some argument from the other regulars here, but except in
> special circumstances I don't like using shift to pull arguments off @_.
> IMHO it's much clearer to put all the arguments you want in named
> variables with one list assignment, like this:
> 
>     my ($input) = @_;
> 
>> 	chomp($input);
>> 	my @numbers = split(/ /,$input);
> 
> These two steps should have been done by the code outside the function.
> (The function is supposed to accept a list of numbers, not a
> space-separated string.) In the case of getting numbers from the command
> line, you won't need to do it at all, since the shell has already done
> it for you.
> 
>> 	my @divided = @numbers;
>> 	my $elem;
>> 	foreach $elem(@divided){
>> 		$elem/=2;
>> 		}
> 
> Hey, that's good! Getting the hang of the aliasing behavior of
> for(each) is one of the less trivial early steps in learning Perl.
> 
> It is worth pointing out that if you'd written this instead:
> 
>     foreach my $elem (@divided) {
> 
> the $elem variable would only exist inside the loop, not afterwards. As
> ever, limiting the scope of a variable is always a good idea when you
> can, since it reduces the risk of some other code picking it up
> unexpectedly.
> 
>> 	my $divided = join(" ",@divided);
>> 	return $divided;
>> }
>> 
>> sub getaverage{
>> 	my $input = shift(@_); chomp($input);
>> 	my @numbers = split(/ /,$input); my $qty = $#numbers +1;
> 
>     my $qty = @numbers;
> 
>> 	my $combined;
>> 	$combined += $_ for @numbers;
>> 	my $average = ($combined / $qty);
> 
> or indeed simply
> 
>     my $average = ($combined / @numbers);
> 
> Using an array where a scalar value is expected ('in scalar context')
> gives you the length of the array. This is logically cleaner, as well as
> simpler, than adding one to the index of the last element.
> 
> [In the old days it was safer as well, because Perl had a way to change
> arrays from being 0-based to being anything-else-based (typically 1).
> This 'feature' was so confusing it was eventually (in 5.16) removed.]
> 
>> 	return $average;
>> }
>> 
>> sub gettotal{
>> 	my $input = shift(@_); chomp($input);
>> 	my @numbers = split(/ /,$input); my $combined;
>> 	$combined += $_ for @numbers;
> 
> Be consistent about whether you write 'for' or 'foreach'. They are
> exactly equivalent, so most people choose to just use 'for', because
> it's shorter.
> 
>> 	return $combined;
>> }
> 
> Now, here is an exercise in thinking like a programmer. Look at this
> function. Look at the previous function. They seem to be very similar,
> don't they? What could you do about that?
> 
>> My question is this. In the above example I take an array and convert
>> it to a string where it is passed to the library where the string is
>> converted back to an array. Then I process my answer, which is
>> converted to a string and returned back to the Perl script.
>> 
>> Can I simply pass an array without conversion to a string??
> 
> You are already, in fact, passing a list, which comes into the function
> as an array; you just happen to have passed a list of only one element.
> @_, despite it's rather odd appearance, is actually just a (mostly)
> perfectly ordinary array variable. So:
> 
>     sub foo {
>         for (@_) {
>             print "$_\n";
>         }
>     }
> 
>     foo 1, 2, 3;
> 
>     my @array = (4, 5, 6);
>     foo @array;
> 
>     foo 3, @array, 7;
> 
> Ben

Much of this mess is because I didn't realize I could just work directly 
on the array. They showed us this one thing which works this way and then 
gave some objectives. So I use it as a model. It's all good though. In 
the end I feel like I understand the topic much better. 

Pain really teaches! 

More to think about. I'll likely start the full course next week if all 
guess well. So that means starting over. Hopefully I'll get more depth 
and the code won't look so patched up. :)

Thanks Ben!!

-- 
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org


------------------------------

Date: Sat, 1 Mar 2014 09:53:19 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: using a library
Message-Id: <slrnlh37vv.dlh.hjp-usenet3@hrunkner.hjp.at>

On 2014-03-01 02:50, Marek Novotny <mach2@hushmail.com> wrote:
> On Sat, 01 Mar 2014 01:10:10 +0000, Ben Morrow wrote:
>> Quoth Marek Novotny <mach2@hushmail.com>:
>>> print "\n";
>>> print "You entered: $input\n";
>>> print "The average of your numbers is: $average\n";
>>> print "The total of all your numbers combined is: $total\n";
>>> print "Each of your numbers divided by 2 are: $div\n\n";
>>> print "Have a nice day!!\n\n";
>> 
>> A great long sequence of print statements is, I'm afraid, one of the
>> classic signs of a Perl beginner :).

So after 19 years of Perl programming I'm still a beginner. Or rather.
I'm a beginner again.

>> Perl has a quoting construction
>> called a 'here document' that is rather clearer; this was inherited from
>> Bourne shell, so if you've learned that you might be familiar with it
>> already:
>> 
>>     print <<OUTPUT;
>> 
>>     You entered: $input The average of your numbers is: $average The
>>     total of all your numbers combined is: $total Your numbers each
>>     divided by 2 are: $div
>> 
>>     Have a nice day!
>> 
>>     OUTPUT
>
> This is awesome. I just wrote a sample script to test it out. That's 
> cool! They made zero mention of this by the way. 
>
>> (Note that I've indented the code in this post for clarity. In a real
>> program, the final OUTPUT marker must appear at the left margin,
>> regardless of the indentation of the surrounding code.)

And not only the final OUTPUT marker must appear at the left margin, but
the contents start at the left margin, too. So you either have to write
code like this:

    if ($foo) {
        for (@bar) {
            if ($_ <= 0) {
                print <<OUTPUT;
Oh, no! The current element of bar is $_,
but foo is $foo.
OUTPUT
            } elsif ($_ == 1) {
                print <<OUTPUT;
Much better. The current element of bar is $_,
and foo is $foo.
OUTPUT
            } else {
                print <<OUTPUT;
Great. The current element of bar is $_,
and foo is still $foo.
OUTPUT
            }
        }
    }

which I consider totally unreadable. Or you end up postprocessing your
strings at runtime, or shunting each into a function of its own or using
some other workaround.


> Believe it or not, I read it that same way, wrote a nice script that did 
> everything they asked for in one shot, and they rejected it. They wanted 
> it split up.

Your instructors are trying very hard to act like real customers. ;-)


>> I may get some argument from the other regulars here, but except in
>> special circumstances I don't like using shift to pull arguments off @_.
>> IMHO it's much clearer to put all the arguments you want in named
>> variables with one list assignment, like this:
>> 
>>     my ($input) = @_;

I agree.

    my ($foo, $bar, @baz) = @_;

is cleaner than

    my $foo = shift @_;
    my $bar = shift @_;
    my @baz = @_;

Maybe just because the former looks more like the parameter list in
other programming languages.

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


------------------------------

Date: Sat, 01 Mar 2014 10:35:27 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: using a library
Message-Id: <bndnv2Fc7vjU1@mid.individual.net>

Am 28.02.2014 19:57, schrieb Marek Novotny:
> use strict;
> use warnings;
>
> require 'obj13-lib.pl';


As usual, I won't answer to your original question.

Right now, I just want to emphasize the convention when working with 
libraries.

In Perl, we usually call them '*.pm',
that is a pretty unique file ending,
that makes it clear, there is a Perl Module, also called a library.
So, the canonical name for your library would be 'Obj13.pm' [usually 
modules are written uppercase].

Usually we would use the library via 'use',
so we'd write

use Obj13;

instead of require 'obj13-lib.pl';

Technical, use XYZ is the same as
BEGIN { require 'XYZ' }
so there is not much difference.
(BEGIN { ... } means it is run first, not at runtime).

In the long run, it is better to get a problem signaled at compile time 
than at run time, as then there might be strange errors.
Usually, we only use require '...' when we want to load a library 
dynamic (e.g. we'd prefer to work with a compiled C library, but if not 
availably we take the pure Perl pendant)


I hope that does not confuse you more,
I just wanted to hint you to the usual conventions,
as it will make your programming life easier long term.



Greetings,
Janek



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4158
***************************************


home help back first fref pref prev next nref lref last post