Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Modern Perl Book "Paquito" Example Question

by Ayber (Novice)
on Oct 05, 2018 at 20:39 UTC ( #1223594=perlquestion: print w/replies, xml ) Need Help??
Ayber has asked for the wisdom of the Perl Monks concerning the following question:

Has found myself in charge of maintaining some Perl web apps for a BioStatistics lab. Decided I should learn more about Perl to do so effectively. Picked up the book, "Modern Perl" 4e.

Have run a few examples from this book, but this one using the "it" doesn't do as expected. All that I have added to the code from the book is the shebang line. The code should print something, not quite sure what. Have tried perl 5.28.0 and 5.10.1 to the same result. The code runs, nothing prints. Am sure that I missing something quite simple here

#!/usr/bin/perl $_ = 'My name is Paquito'; say if /My name is/; s/Paquito/Paquita/; tr/A-Z/a-z/; say;

Code will run from terminal with no errors, no text is said in terminal. What context am I missing? Thank you for your time

Replies are listed 'Best First'.
Re: Modern Perl Book "Paquito" Example Question
by hippo (Canon) on Oct 05, 2018 at 21:52 UTC

    I have the first version of Modern Perl to hand. In page 9 of the PDF (numbered page ii - the first page of the preface) it says this:

    Unless otherwise mentioned, code snippets always assume the basic skeleton of a program:
    #!/usr/bin/perl use Modern::Perl; # example code here ...

    but you have not done this so you encounter the error reported.

Re: Modern Perl Book "Paquito" Example Question
by stevieb (Abbot) on Oct 05, 2018 at 20:57 UTC

    Welcome to the Monastery, Ayber!

    This is a perfect example of why *all* of your code should have use warnings; and use strict; at the very top. It would point you to the err of your ways:

    use warnings; use strict; $_ = 'My name is Paquito'; say if /My name is/; s/Paquito/Paquita/; tr/A-Z/a-z/; say;

    Output:

    Bareword "say" not allowed while "strict subs" in use at it.pl line 5. Bareword "say" not allowed while "strict subs" in use at it.pl line 10 +. Execution of it.pl aborted due to compilation errors.

    The say() function isn't available by default, which is why nothing happens. Without use strict;, no errors are triggered/displayed, and the code blindly just carries on skipping the say() lines.

    To fix the problem, add either use feature 'say';, use v5.10; or use Modern::Perl to include the function:

    use warnings; use strict; use feature 'say'; $_ = 'My name is Paquito'; say if /My name is/; s/Paquito/Paquita/; tr/A-Z/a-z/; say;

    Output:

    My name is Paquito my name is paquita

    You could also avoid requiring external functions if you changed say() to print():

    use warnings; use strict; $_ = 'My name is Paquito'; print "$_\n" if /My name is/; s/Paquito/Paquita/; tr/A-Z/a-z/; print "$_\n";

    Note I've left the use of the default variable ($_) in place. Most definitely not the way I'd use it in any normal situation, but I digress.

Re: Modern Perl Book "Paquito" Example Question
by Perlbotics (Chancellor) on Oct 05, 2018 at 20:58 UTC

    perldoc -f say

    ... "say" is available only if the "say" feature is enabled or if it is prefixed with "CORE::". The "say" feature is enabled automatically with a "use v5.10" (or higher) declaration in the current scope.

    So you could add

    use v5.10; # or higher ...
    or
    use feature qw(say); ...
    or
    $_ = 'My name is Paquito'; CORE::say if /My name is/; s/Paquito/Paquita/; tr/A-Z/a-z/; CORE::say;

    Had you add use strict; the following warning would have been issued hinting that say is unknown:

    Bareword "say" not allowed while "strict subs" in use at ./say.pl line + xyz.

Re: Modern Perl Book "Paquito" Example Question
by LanX (Archbishop) on Oct 05, 2018 at 20:49 UTC

      Thank you the quick replies, the answer was to:

      use Modern::Perl
        well - if installed - that's what Modern::Perl does among other things:

        activating features and strictures.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Modern Perl Book "Paquito" Example Question
by soonix (Abbot) on Oct 05, 2018 at 20:52 UTC
    say is not predefined by default, you have to enable it, e.g. with
    use feature 'say';
    or (better)
    use Modern::Perl;
    or …
Re: Modern Perl Book "Paquito" Example Question
by Happy-the-monk (Canon) on Oct 05, 2018 at 20:51 UTC

    What context am I missing?

    There needs to be a use feature qw(say); near the top of the programme.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1223594]
Approved by davies
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2018-12-17 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (73 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!