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

Bareword rules?

by cheekuperl (Monk)
on Aug 23, 2012 at 04:55 UTC ( #989197=perlquestion: print w/ replies, xml ) Need Help??
cheekuperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
Are there any rules for bareword to string conversion? I am perplexed to see different behaviors as below:
$ perl -e 'print cat' # Doesn't print anything. But 1 bareword is OK $ perl -e 'print cat dog' #Doesnt like two barewords Can't locate object method "cat" via package "dog" at -e line 1. $ perl -e 'print reverse cat' #Prints cat. OK with one again. cat$ perl -e 'print reverse cat dog' # Still hates two Can't locate object method "cat" via package "dog" at -e line 1.
This is perl, version 5.004_04

Comment on Bareword rules?
Download Code
Re: Bareword rules?
by davido (Archbishop) on Aug 23, 2012 at 05:19 UTC

    Perl version 5.004_04? Where did you get that?

    • perl -e 'print cat': "cat" is treated as an unopened file-handle (a typeglob) in package 'main', and compiled as print cat $_.
    • perl -e 'print cat dog': Indirect object syntax (the syntax that many wish would go away. It's Perl's equivalent of "the most vexing parse"). See perlobj#Invoking-Class-Methods. Compiles as print dog->cat;
    • perl -e 'print reverse cat': 'reverse' is treated as a function, and 'cat' is treated as a simple unquoted string. Compiles as print reverse('cat'); reverse is called in list context, so you don't see much happening since there's only one argument.
    • perl -e 'print reverse cat dog': There's the most vexing parse again: Compiles as print reverse dog->cat;

    Perl probably has plenty of other "most vexing parses", so I shouldn't really direct that phrase at indirect object syntax in particular, but it's one easy target.

    5.004_04      1997-Oct-15: Do you remember what specs your computer had in 1997? I think I had a Pentium-I with a 2GB hard drive and maybe 128MB RAM.


    Dave

      Thanks for the explanation. I get it now :)
      This version of Perl is default installation on a Linux 2.6 machine in my office. We do use 5.10 though for all work purpose.
      And I didn't have a computer back in 1997 :)
Re: Bareword rules?
by moritz (Cardinal) on Aug 23, 2012 at 05:44 UTC

    print is a bit special, since it expects a file handle as an optional first argument, which can be a bareword.

    You can eliminate that speciality by adding an explicit STDOUT:

    $ perl -le 'print STDOUT one, two, three' onetwothree

    So you can have as many barewords in term position as you like.

    If you leave out a comma, you have indirect method call syntax instead:

    $ perl -le 'print STDOUT one, two three' Can't locate object method "two" via package "three" (perhaps you forg +ot to load "three"?) at -e line 1.
      Okay!! Thanks!!
Re: Bareword rules?
by mr.nick (Chaplain) on Aug 24, 2012 at 15:18 UTC
    Just as a quick comment, Deparse is very useful when trying to determine what the parser is actually doing:
    $ perl -MO=Deparse -e 'print cat dog' print 'dog'->cat; -e syntax OK $ perl -MO=Deparse -e 'print reverse cat dog' print reverse('dog'->cat); -e syntax OK

    I'm going to ASSUME that 5.004 has this ability, but considering it's decades old, maybe not.

    mr.nick ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-04-18 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls