Re: using a library

Discussion in 'Perl Misc' started by Peter J. Holzer, Feb 28, 2014.

  1. On 2014-02-28 18:57, Marek Novotny <> wrote:
    > Another weird question. I am going over how to use a Perl script in which
    > I send the process to a library to process and then the library returns
    > the answer:
    >
    > This is one of my Perl scripts:
    >
    > --begin--------------------------------------

    [...]
    > my $input = <STDIN>;
    >
    > my $average = getaverage($input);
    > my $total = gettotal($input);
    > my $div = getdivby2($input);

    [...]
    > --end---------------------------------
    >
    > And it makes use of this library:
    >
    > --begin------------------------------------------

    [...]
    > sub getdivby2{
    > my $input = shift(@_);
    > chomp($input);
    > my @numbers = split(/ /,$input);
    > my @divided = @numbers;
    > my $elem;
    > foreach $elem(@divided){
    > $elem/=2;
    > }
    > my $divided = join(" ",@divided);
    > return $divided;
    > }

    [...]
    > -----------end-------------------------
    >
    > 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


    You do? Where? I see only $input being passed "to the library" (I would
    say it is passed to the functions in the library), and that is read from
    stdin, not converted from an array.

    > 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??


    Yes. Indeed you can see a hint in the first line of your functions:

    > 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

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

  2. Peter J. Holzer

    John Bokma Guest

    John Bokma, Mar 1, 2014
    #2
    1. Advertising

  3. Peter J. Holzer

    John Bokma Guest

    Marek Novotny <> writes:

    > On Fri, 28 Feb 2014 18:11:10 -0600, John Bokma wrote:
    >
    >> Marek Novotny <> 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
    John Bokma, Mar 1, 2014
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mythran
    Replies:
    0
    Views:
    2,420
    Mythran
    Aug 24, 2004
  2. Alan Ferrandiz [MCT]
    Replies:
    0
    Views:
    445
    Alan Ferrandiz [MCT]
    Sep 11, 2004
  3. Sweep

    Library in library...

    Sweep, Dec 8, 2003, in forum: C++
    Replies:
    1
    Views:
    383
    Jack Klein
    Dec 9, 2003
  4. Replies:
    6
    Views:
    830
    red floyd
    May 10, 2005
  5. David Soukal

    library using library?

    David Soukal, May 14, 2004, in forum: C Programming
    Replies:
    10
    Views:
    620
    Brian Gough
    May 17, 2004
Loading...

Share This Page