http://qs321.pair.com?node_id=667469

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

So I'm teaching a Fundamental Programming class.

I tell my students to do:

$string = 'hello world'; print substr($string,0,5);
But what one guy does is:
$string = 'hello world'; print substr($string,O,5);
(with a letter O instead of a zero, in case you can't quite make it out).

The trouble is ... it still worked. Why is this? I guess it's parsing the unquoted string for number-like-ness, getting nothing and returning zero, which is all very well, but I've turned on warnings, and there isn't one!

UPDATE: I was wrong about the warnings! I feel rather embarrased. Running the code in a particular IDE didn't give a warning, but running it from the command line does.



Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re: Barewords equal zero?
by Joost (Canon) on Feb 11, 2008 at 22:53 UTC
    Turning on warnings gives:
    Argument "O" isn't numeric in substr at test.pl line 2. hello

    Turning on strict & warnings gives:

    Global symbol "$string" requires explicit package name at test.pl line + 1. Global symbol "$string" requires explicit package name at test.pl line + 2. Bareword "O" not allowed while "strict subs" in use at test.pl line 2. Execution of test.pl aborted due to compilation errors.
    update: and yeah, without strict, barewords are interpreted as strings, and strings that don't start with numbers are converted to 0 in numeric context. Which should give a warning with warnings enabled.

Re: Barewords equal zero?
by GrandFather (Saint) on Feb 11, 2008 at 22:58 UTC

    Fundamental to learning and using Perl effectively is to always use strictures (use strict; use warnings;) as you have now discovered. ;)


    Perl is environmentally friendly - it saves trees
      You seem not to have read my post.


      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...

        You seem to have updated your post since I read it.

        The fact that the assignment line isn't prefixed with my is a strong hint that you weren't using strictures. What you show people is what they will do. Show them strictures right from the start - even for trivial code like your sample. At the very least assume strictures and stick a my in front of the assignment.


        Perl is environmentally friendly - it saves trees
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Barewords equal zero?
by syphilis (Archbishop) on Feb 12, 2008 at 06:39 UTC
    Running the code in a particular IDE didn't give a warning

    It might therefore be a good idea to avoid that IDE.

    Cheers,
    Rob
      Or rather use strict; to catch this errror, even when in an IDE. I doubt any IDE implements it's own Perl, so it will have to rely on ouput from the underlying Perl installation, which in turn relies on you to tell it what to do.
      It would be a better idea to avoid IDEs altogether when teaching newbies. Even *Notepad* is sufficient for noddy programs like this, and you can guarantee that it won't hide any essential information - such as warnings.

        That's very macho and all, but the reductio ad absurdum is to issue the students magnetized needles, or perhaps to hold class in a butterfly farm. IMHO, a good IDE with syntax coloring is immensely helpful at catching errors early. In fact, it helps me when I'm typing without having to wait until compile time (as with strictures). Without any empirical proof I'd conjecture that when teaching newbies it has instructive value even "for noddy programs like this".


        #my sig used to say 'I humbly seek wisdom. '. Now it says:
        use strict;
        use warnings;
        I humbly seek wisdom.
Re: Barewords equal zero?
by rgiskard (Hermit) on Feb 12, 2008 at 18:54 UTC

    An example for the lazy folks out there, that demonstrates (for beginners) why warnings would be useful in addition to strict.

    using strict only
    use strict; my $string = 'hello world'; print substr($string,'O',5)."\n";
    Output doesn't really scream don't use the letter O:
    perl absurd.pl hello
    using strict and warnings
    use strict; use warnings; my $string = 'hello world'; print substr($string,'O',5)."\n";
    Useful output with some notes on what should be a number and isn't:
    perl absurd.pl Argument "O" isn't numeric in substr at absurd.pl line 4. hello

      I noticed you changes the OP's code to something that strict wouldn't catch...

      But you're right.
      strict will catch some things.
      warnings will catch others.
      Both have their uses.

        In the previous example, I put quotes around the capital letter O. Because, quite frankly, barewords are painful.

        It's also important to look at it from the perspective of someone fixing the barewords but not fixing the real problem, as some may read the capital letter O and think it to be a zero (0) and brute force the solution by getting rid of the barewords by quoting everything.

        So without warnings, there's no difference between a quoted letter O and a quoted number 0. If one is starting out with the language then hopefully I've pointed out some positives about strict and warning.

        example with a quoted number zero
        use strict; use warnings; my $string = 'hello world'; print substr($string,'0',5)."\n";
        output
        sh-3.2$ perl absurd2.pl hello
Re: Barewords equal zero?
by Cody Pendant (Prior) on Feb 14, 2008 at 01:54 UTC
    Just for the record, I don't teach using any IDE.

    I teach using Text-Edit and the command line.

    And I didn't teach them to use strict and warnings in the very first class, because, despite the fact that I know all about them and use them all the time, that was simply too much information.

    We're talking about people who did not know what the command line is, and every mistake they made was at such a basic level that compile failed. Using "Print" instead of "print", getting quotes, brackets, commas wrong, not knowing what a semicolon looks like, etc., seriously. I'm almost proud that someone got made a mistake so minimal as an O instead of a zero. Most of the time they couldn't remember which window was the script and which window was the Terminal.

    Fun.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      "And I didn't teach them to use strict and warnings in the very first class, because, despite the fact that I know all about them and use them all the time, that was simply too much information."

      I still remember my very first Perl "lesson". The very first thing I was told was:

      "Every Perl program you ever write will contain the lines use strict; and use warnings;. Do not ask why, just accept this for now and do it".

      I'm so glad I was told this, and I think that anyone that is just starting out learning Perl and is not being told this is being done a dis-service.

      So I disagree with you. It is not too much information at all. It is probably one of the most vital bits of information a newcomer needs, and definitely should be included in the first lesson.

      Note that I'm not advocating that you try to explain why strictures and warnings are useful, or when they could be avoided. This would definitely be too much information. People can find this out for themselves at a later stage. What's most important is to get them into good habits right from the start. Where strictures are concerned, I'd say it's a case of "Use them because I tell you to. When you've learned enough to understand why they are useful, then you can decide for yourself."

      Cheers,
      Darren :)

Re: Barewords equal zero?
by Tacitus (Novice) on Feb 12, 2008 at 04:43 UTC
    cool :-)
Re: Barewords equal zero?
by ambrus (Abbot) on Feb 18, 2008 at 08:40 UTC

    On the other hand, if you use such a word not as a number or integer but a boolean, they count as true unlike a zero would.