tricky use of print?

tricky use of print?

am 02.04.2008 08:57:52 von ela

I found that little can be done on debugging a variable on print, after
visiting a page containing the module PadWalker.

I wonder whether in Perl can do something like:

$newline = '\n'

print
foo
$foo

print $newline;


I use the vim editor, in this sense, rapid coding and debugging can achieve.
But i at least know that the newline trick doesn't work...

Re: tricky use of print?

am 02.04.2008 09:06:09 von David Filmer

Ela wrote:
> $newline = '\n'

In Perl, single-quotes are not interpolated (meaning $newline is set to
backslash-n). You would need to use double-quotes (or qq{}) to
interpolate \n as a newline.

Re: tricky use of print?

am 02.04.2008 18:11:04 von szr

David Filmer wrote:
> Ela wrote:
>> $newline = '\n'
>
> In Perl, single-quotes are not interpolated (meaning $newline is set
> to backslash-n). You would need to use double-quotes (or qq{}) to
> interpolate \n as a newline.

Or a heredoc :-)

print <<_EOF_;
$foo $bar
_EOF_

--
szr

Re: tricky use of print?

am 02.04.2008 18:20:56 von ela

> Or a heredoc :-)
>
> print <<_EOF_;
> $foo $bar
> _EOF_
>
> --
> szr
>

There are too few words in your example and I'm unable to follow/Google. I
guess maybe you are telling something important? thx

Re: tricky use of print?

am 02.04.2008 19:28:54 von David Filmer

Ela wrote:
>> Or a heredoc :-)
>>
>> print <<_EOF_;
>> $foo $bar
>> _EOF_
>>
>
> There are too few words in your example and I'm unable to follow/Google. I
> guess maybe you are telling something important? thx

szr is just showing you an example of how to use a heredoc, which
recognizes the \n at the end of any lines within it (so it's just
another way of printing newlines).

You could do the same thing like this:

print "$foo $bar
";

but that's questionable programming style (whereas a heredoc is
generally accepted, though I personally dislike them).

Re: tricky use of print?

am 02.04.2008 23:28:24 von szr

David Filmer wrote:
> Ela wrote:
>>> Or a heredoc :-)
>>>
>>> print <<_EOF_;
>>> $foo $bar
>>> _EOF_
>>>
>>
>> There are too few words in your example and I'm unable to
>> follow/Google. I guess maybe you are telling something important? thx
>
> szr is just showing you an example of how to use a heredoc, which
> recognizes the \n at the end of any lines within it (so it's just
> another way of printing newlines).

I should of also said, you can either physically have new lines, like:

print <<_EOF_;
A
B
C
_EOF_


Or you can use \n instead:

print <<_EOF_;
A\nB\nC
_EOF_


Or mix and match:

print <<_EOF_;
A
B\nC
_EOF_


Either way you get:

$ perl -e 'print <<_EOF_;
> A
> B\nC
> _EOF_'
A
B
C

--
szr

Re: tricky use of print?

am 03.04.2008 01:09:02 von Tad J McClellan

Ela wrote:
>> Or a heredoc :-)
>>
>> print <<_EOF_;
>> $foo $bar
>> _EOF_
>>
>> --
>> szr
>>
>
> There are too few words in your example and I'm unable to follow/Google. I
> guess maybe you are telling something important? thx


See the "<

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"