harangzsolt33 has asked for the wisdom of the Perl Monks concerning the following question:

I started learning Perl not too long ago (in 2016), and I am trying to get to a higher level. At least, I want to get familiar with every builtin function. Is there a website that explains what each builtin function does and how to use it with a little demo program that one can test?

I copied all the functions from perldoc and put them in an Excel file, and then I organized them into two groups -- the ones that I understand already and the ones that I don't understand. Then I broke them down into futher sub groups. These are the ones that I have used before and somewhat understand how they work and what they do:

FLOW-CONTROL:
goto sub if elsif else for foreach while continue next last return do die exit sleep kill system exec and or && || ? :

STDIO AND FILE SYSTEM I/O:
open print printf sysopen sysseek binmode eof rename chmod close unlink getc read stat utime seek tell sysread syswrite truncate opendir chdir closedir mkdir rmdir readdir rewinddir -e -f -s -d

MATH:
abs int ord srand rand exp cos sin sqrt ++ -- + - / * << >> == *= /= |= &= != >= <= < > | ^ & ! % ~

STRINGS:
substr length vec chomp chop pack unpack hex oct chr undef sprintf uc lc index rindex eq ne qw tr y s m x =~

LISTS:
map grep splice reverse push pop shift unshift sort split join

VARIABLES:
defined exists ref my scalar caller wantarray @_ @ARGV $_ $a $b $0 __FILE__ $^O $^T $^W $| $$ $[ $] $/ $ENV{NAME} $1 $2..

TIME:
localtime time

OTHER:
use package import eval require warn __END__ __DATA__


Now, these are the ones that I don't understand:

Never used these and not sure:
atan2 q qx qq qr chown reset gmtime fork link symlink local dump bless our pos readline readlink syscall write format seekdir telldir lock prototype formline getppid keys log

Never used these and have no clue what they do or what purpose they serve:
accept endgrent endhostent endnetent endpwent endprotoent endservent tie bind untie dbmclose shutdown connect socketpair socket dbmopen delete readpipe glob fcntl getpeername tied getpriority getgrnam getgrgid gethostbyaddr gethostbyname getnetbyaddr getnetbyname getgrent gethostent getnetent getpwent getprotoent getservent getpwuid getpwnam getpgrp getprotobyname getprotobynumber getservbyname getservbyport semget getsockopt msgget shmget flock chroot m crypt pipe study setgrent sethostent setnetent setpwent setprotoent setservent quotemeta shmread recv msgrcv listen select each getsockname values lcfirst ucfirst times fileno getlogin alarm send msgsnd setpriority umask setsockopt setpgrp redo lstat ioctl msgctl semctl semop shmctl no waitpid wait shmwrite

Replies are listed 'Best First'.
Re: How to find Perl demo programs?
by Fletch (Bishop) on May 21, 2023 at 05:41 UTC

    A lot of your things you don't understand are wrappers around or versions of underlying standard *NIX system calls and/or library functions. A good reference such as APUE (ISBN 9780321525949) will explain those. The perl version will usually do the same thing (in a perl-y way; taking a handle rather than a file descriptor (e.g.)).

    Additionally The Perl Cookbook (ISBN 9780596003135), while a little long in the tooth, also provides more of these simple samples that you're wanting.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: How to find Perl demo programs?
by eyepopslikeamosquito (Archbishop) on May 21, 2023 at 06:40 UTC
Re: How to find Perl demo programs?
by kcott (Archbishop) on May 21, 2023 at 04:16 UTC

    G'day harangzsolt33,

    "Is there a website that explains what each builtin function does ..."

    Yes: https://perldoc.perl.org/perl.

    "... and how to use it with a little demo program that one can test?"

    Most of the entries in perldoc have examples (or links to other manpages with examples).

    If you're looking for specific tests, try https://github.com/Perl/perl5. You can pick a specific branch or tag. The default is "blead" but, let's say you wanted the latest stable release (at the time of writing), you'd pick the v5.36.1 tag and get https://github.com/Perl/perl5/tree/v5.36.1. From here you can select the "t" directory where you'll find all of tests: check the README and drill down into subdirectories for specific tests.

    "I copied all the functions from perldoc ..."

    Perhaps you need to be more specific. To me, that means you looked in https://perldoc.perl.org/functions; however, if you'd done that, you wouldn't be asking where to find function documentation.

    There's also a Search function in the top-righthand corner of https://perldoc.perl.org/perl.

    — Ken

      Well, I know the documentation exists, but it does not have sufficient example programs in it. For example, let me show you what I mean. This page talks about the chr() function: https://perldoc.perl.org/functions/chr but if you open it, there is no example program. This is what I would expect an example program to look like:

      #!/usr/bin/perl use strict; use warnings; print 'A'; # Prints 'A' print chr(65); # Prints 'A' my $c = 65; print chr($c); # Prints 'A' $_ = 65; print chr; # Prints 'A'

      An example program or demo program is something that you can just copy and paste, and it works. And it shows you what one particular keyword or function can do and how it's used. The functions are described in the documentation in pretty good detail, but a working example is usually missing.

      But thank you, I will study the GitHub link you provided. It looks like it might be what I am looking for, although it's quite messy.

        This is what I would expect an example program to look like:
        #!/usr/bin/perl use strict; use warnings; print 'A'; # Prints 'A' print chr(65); # Prints 'A' my $c = 65; print chr($c); # Prints 'A' $_ = 65; print chr; # Prints 'A'

        I'll assume that you've successfully tested that on your system; however, consider:

        • Not all systems have /usr/bin/perl.
        • chr() does not require the strict pragma.
        • chr() works on versions of Perl5 prior to the introduction of the warnings pragma.
        • A variable, supplied as an argument to chr(), does not need to be lexical.

        So, as you can probably see, there's no one-size-fits-all example. Providing explanations and code fragments is likely to be suitable for a wider audience; even then, many pages require links to perlport or other caveats.

        "I will study the GitHub link ... although it's quite messy."

        Well, I didn't have any trouble finding the chr() tests for the latest stable release, https://github.com/Perl/perl5/blob/v5.36.1/t/op/chr.t, or those from over a decade ago, https://github.com/Perl/perl5/blob/v5.16.0/t/op/chr.t.

        At the end of the day, if you're really struggling with using some function, ask here: we're always happy to help. I would request that you restrict your questions to a single function or related group of functions; listing every operator and function that you can find is just off-putting — many who could otherwise provide insightful responses may simply move on to the next question.

        — Ken

Re: How to find Perl demo programs?
by LanX (Saint) on May 21, 2023 at 10:49 UTC
    Some remarks:

    links

    It would be easier for all of us if you linked to those perldocs.

    That's quite easy, just edit your post and and run a regex to surround each missing command with [doc://...]

    like atan2 -> atan2

    grouping

    The next point is classification

    getservbyname and many others belongs to the group Fetching-network-info

    Assembling the commands in such groups would facilitate the task a lot !!!

    They were originally classified in the perlfunc you copied, why did you strip it again?

    Compare "divide and conquer"

    snippets

    Next: After reading Re^2: How to find Perl demo programs? I assume you rather mean usage snippets than full demo programs.

    It would be very nice to have such snippets for many reasons.

    • IDEs could profit from suggesting them
    • The behavior of Perl is often underdefined
    • The docs are often overly complex, the main info get lost in sometimes cryptic mentions of edge cases.
    • Perl is undertested, such snippets could be used to catch regressions from version to version
    I once tried an automatic approach to generate them, but alas life came in-between.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      They were originally classified in the perlfunc you copied, why did you strip it again?

      Oh wow. I didn't see that. I have been using an older version of perldoc which is downloaded on my computer, and the functions are listed in alphabetical order in that version. I didn't know that this list is now sorted by category. That's pretty cool!

Re: How to find Perl demo programs?
by BillKSmith (Monsignor) on May 22, 2023 at 13:22 UTC
    I do not think it is a good idea to even try to learn all of Perl. Very few of us ever use all of it. Think of Perl as a giant tool kit. Learn well the tools you need for your current requirements. Your post suggests that you have already reached this point. Use the hints other monks have provided to add tools to you kit as your needs or interest dictate.

    I know that there is a down side to this approach. Occasionally you will discover a new tool and think "Wow, I wish I had known this last week." But remember, you did solve the problem and make a working program. Isn't that better than waiting forever for the day that you could do it perfectly with some seldom used tool? Never stop learning!

    Bill
Re: How to find Perl demo programs?
by NERDVANA (Priest) on May 22, 2023 at 21:36 UTC
    While most of your list are perl wrappers for C-library stuff (and any research you do into that is just generally useful for becoming a Unix expert) the ones that stand out to me are q qx qq qr.

    Those aren't functions, they are one of Perl's syntactic superpowers! (that I wish other languages would start using) I agree that the documentation for them is rather terrible in perldoc perlfunc. You can find a much better explanation at perldoc perlop under Quote-Like-Operators.

    The gist is that while you can just use backslashes to have i.e. double-quotes inside of a double-quoted string, it often looks terrible, so perl gives you the 'qq' syntax that lets you choose your own terminating character for the string. Then you can choose one that doesn't interfere with the readability of your string.

    It makes even more difference when you have strings inside of strings, like:

    eval qq{ print qq{This string uses "double quotes"\n}; }
    instead of
    eval " print \"this string uses \\\"double quotes\\\"\n\"; "

    A warning though, you probably shouldn't use 'qx' in production code. It lets you choose your own quotes for ` ... ` but any time you have something complex enough that you wanted alternate quote characters to pass something to bash to execute, you are probably opening yourself up to shell injection attack vectors. It's much better to run external programs with system("progname", $arg1, $arg2, ...) where you are in full control over the boundaries of the arguments to the program, and not relying on the shell to re-parse them based on quote characters. You can capture the output of system() using Capture::Tiny.

    **Edit**

    So also I see 'keys', 'values', and 'each' on your list. Those are fairly important perl functions for working with hashes. And... actually the documentation is fairly good for those, so I'm not sure what else to say about them. You should practice working with them until you get the feel for it.

    Finally, 'pos' is a more advanced perl feature, but lots of fun if you're writing parsers. Basically, every Perl string has a built-in position marker, and the perl regex engine can make use of that position. The 'pos' function lets you access or change the position marker. And that's really all you need to know until you decide to write a fancy parser. Then you can come back and ask more questions :-)

      Yeah, I haven't done anything with hashes except %ENV, but I will have to get more familiar with them. And the qq{} syntax was confusing at first. I've seen others use it, but I wasn't sure how it worked. I think I'll have to take a second dive into a Perl tutorial once again. But thank you for the explanations!