Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

For the 'print' function in perl v5.14

by Diamondust (Novice)
on Sep 24, 2012 at 17:11 UTC ( #995421=perlquestion: print w/replies, xml ) Need Help??
Diamondust has asked for the wisdom of the Perl Monks concerning the following question:

Solved. It is a wrong .bashrc file that gives an alias adding the -l switch...

In perl v5.10, as pre-installed on my Ubuntu 64bit 10.04 LTS OS, the 'print' function does not append "\n" after it automatically (which is the right behavior). So the following command:

$ /usr/bin/perl -e 'print 123; print 456'       # v5.10

will print:

123456                                          # no NL appended

But in perl v5.14 in my own folder, 'print' behaves exactly the same as 'say', i.e. it prints a "\n" after it every time:

$ perl -e 'print 123; print 456'                # v5.14

gives the output:

123 # NL appended 456

The website says that the only difference between 'print' and 'say' is that 'say' appends NL after the string list while 'print' does not.

Is this a bug behavior of 'print' in perl v5.14 then?

Replies are listed 'Best First'.
Re: Bug for the 'print' function in perl v5.14?
by Corion (Pope) on Sep 24, 2012 at 17:36 UTC

    Are you certain that perl -e '...' is not an alias for something else? perlrun lists options that could have been used to make print output a newline every time.

    I recommend checking for shell aliases (and understanding or killing them), and invoking Perl via ./perl or the appropriate path to each Perl version.

      You are right...

      While running perl directly prints out NL every time, going to perl5.14.2's parent directory and running ./perl behaves normally (no NL is printed.)

      For some reason there is a line aliasing perl to

      alias perl='perl -l'

      in my .bashrc file.

      I've deleted it, and everything works fine now.


Re: Bug for the 'print' function in perl v5.14?
by toolic (Bishop) on Sep 24, 2012 at 17:26 UTC
    My perl v5.14.2 behaves as expected (no newlines,say). You should also show the perl version:
    perl -e 'print 123; print 456; print $]' 1234565.014002 perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-li +nux-thread-multi
      perl -e 'print 123; print 456; print $]' Shows: 123 456 5.014002
      perl -v Shows: This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-li +nux
Re: Bug for the 'print' function in perl v5.14?
by tobyink (Abbot) on Sep 24, 2012 at 19:21 UTC

    print will print its arguments and then print the output record separator.

    What's the output record separator? It's the variable $\ which is the empty string by default. It looks like something is setting your output record separator to a line break.

    Things to check:

    • When you run perl at the command line, is that really running the usual Perl executable? Or is it triggering a shell alias; or a wrapper script?

    • Try running this command:

      perl -MConfig -E'say for grep -e, qq[$Config{sitelib}/]'

      If it prints out a filename, then take a peek inside that file. See if it's doing anything unusual.

    update: replaced checking command with a more aesthetically pleasing, but functionally identical one

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      perl -MConfig -E'$_="$Config{sitelib}/"; -e()?say:()'

      This command prints nothing for me, but thank you all the same!

Re: Bug for the 'print' function in perl v5.14?
by frozenwithjoy (Priest) on Sep 24, 2012 at 17:26 UTC
    Which version of 5.14? perl -e 'print 123; print 456' in v5.14.2 works as expected for me (no new-line).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995421]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-04-25 20:44 GMT
Find Nodes?
    Voting Booth?