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

I've tried to get into the mysteries of the strict pragma, but it seems that there are some miracelous things happening - at least I do have this impression.

#!usr/bin/perl use strict; # prints 'nono1', so '=>' seems to be interpreted as '.' print nono => "1"; # just ok though print nono . "1"; # Error as expected : No comma allowed after filehandle ... print nono, 1;

The docs for strict state that barewords don't raise errors if left of the '=>' operator, but ...

My question is, why is the assignment operator '=>' in the first 'print' statement here interpreted as a dot and not as a comma as I would expect it as this is how it gets interpreted in a hash key=>value assignment?

Sorry if I seem a bit stupid, but I better ask before I tend to remember wrong relations.

Have a nice day
All decision is left to your taste

Replies are listed 'Best First'.
Re: strict subs and bareword exceptions
by dws (Chancellor) on Feb 22, 2002 at 08:23 UTC
    # prints 'nono1', so '=>' seems to be interpreted as '.' print nono => "1";
    Not quite. Consider   print qw(nono 1); It's more the case that '=>' is a special case of ',' that forces the left-hand side to be a string, even though it looks like a bareword.

Re: strict subs and bareword exceptions
by dvergin (Monsignor) on Feb 22, 2002 at 08:25 UTC
    print nono => "1";   is not the same as
    print nono, 1;   or even
    print nono, "1";

    That is because the "=>" also quotes the bareword that comes before it.
    The equivalent non-"=>" statement would be:

          print 'nono', "1";

    In the statement:
          print nono => "1";
    the "=>" is not acting as a dot. Rather it really is acting as a comma (plus the quoting of the preceding bareword). The two values ("nono" and "1") are simply being printing one after the other.

    Mind you, at some deeper level, the list following the print statement is being concatenated. But I would encourage you not to make explanations about "=>" becoming a dot in that situation. This will just confuse things.

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

Re: strict subs and bareword exceptions
by demerphq (Chancellor) on Feb 22, 2002 at 10:24 UTC
    Try setting the global var $, before you do your prints. I think you'll find the results interesting..
    use strict; $,="---"; $\="\n"; print nono => "1"; print nono . "1"; __DATA__ nono---1 nono1
    Incidentally print LIST is syntactically equivelent to
    print join($, , LIST).$\;
    where LIST is defined in the perlfunc et al...

    Just thought this was a reasonable addition to the other replies...

    Yves / DeMerphq
    --
    When to use Prototypes?

Re: strict subs and bareword exceptions
by cLive ;-) (Prior) on Feb 22, 2002 at 08:22 UTC
    print 'perhaps ', 'Perl ', 'print\'s ', 'a ', 'list '; print 'instead '.'of '.'concatenating?';

    Also, note the difference between these:

    print @array; print "@array";

    .02

    cLive ;-)

    --
    seek(JOB,$$LA,0);

Re: strict subs and bareword exceptions
by rjray (Chaplain) on Feb 22, 2002 at 08:36 UTC

    First and foremost, => is not an assignment operator. It is an alias for "," that has a special trait in which it will quote a bareword string on the left-hand side. So your first line is more like this:

    print 'nono', 1;

    And when print is given a list, it prints all the elements packed together with no gaps inbetween. Which is why you saw "nono1" as output from that.

    You are hereby remanded to read the Perl Operators manpage.

    --rjray

Re: strict subs and bareword exceptions
by jmcnamara (Monsignor) on Feb 22, 2002 at 08:37 UTC

    My question is, why is the assignment operator '=>' in the first 'print' statement here interpreted as a dot and not as a comma.

    It isn't. It is being interpreted as a comma that quotes the string on its left hand side. B::Deparse shows what going on:

    perl -MO=Deparse -e 'print nono => "1";' print 'nono', '1';
    In the second example strict doesn't care about the bareword nono (see below). Nevertheless, this would generate a warning with -w.

    In the last example the bareword looks like a filehandle. And since a filehandle shouldn't have a comma after it in a print statement the compiler, not strict, rejects it.

    --
    John.

Re: strict subs and bareword exceptions
by jmcnamara (Monsignor) on Feb 22, 2002 at 09:39 UTC

    I guess that in the second example the bareword isn't rejected by strict because it looks like a filehandle. The same bareword throws an error if its position is changed:
    # Program Output Deparse perl -Mstrict -le 'print nono . "1" ' # nono1 # print 'nono' . ' +1'; perl -Mstrict -le 'print "1" . nono ' # (error) #

    Unrelated to this, the following seems a little odd:

    # Program Output Deparse perl -Mstrict -le 'print nono . '1' ' # nono1 # print 'nono' . 1 +; perl -Mstrict -le 'print nono .'1' ' # (nothing) # print nono 0.1;

    --
    John.

      The odd one-liners might be due to shell quoting... Can you get the same deparsed output with a regular script?
      $ perl -Mstrict -MO=Deparse -le 'print nono .'1' ' print nono 0.1; $ echo "print nono .'1' " > oneliner $ perl -Mstrict -MO=Deparse -l oneliner print 'nono' . '1';

      -Blake


        You're right. It is the shell. :-)

        --
        John. "Aaargh. The shells, the shells!"