develooper Front page | perl.perl5.porters | Postings from October 2003

Re: Days in confusion

Thread Previous
From:
Michael G Schwern
Date:
October 17, 2003 18:30
Subject:
Re: Days in confusion
Message ID:
20031018012939.GG6990@windhund.schwern.org
On Fri, Oct 17, 2003 at 07:16:59PM +0200, Marcus Thiesen wrote:
> I know I'm quite confused, but after a whole day writing Java you get really 
> confused. I wrote today:
> 	use strict;
> 	use Curses::UI;
> 
> 	my Curses::UI $cui = new Curses::UI;

Yep, that's valid Perl.

The left hand side is the "my Dog $spot" lexical type syntax that never
really got off the ground.  In theory, $cui now has the type "Curses::UI".
In reality, its a no-op.

The right hand side is indirect object syntax, which Perl supports.

So you effectively wrote:

	my $cui = Cures:::UI->new;


> O.K. perl somehow bails out. Next try.
> 	use strict;
> 	use Curses::UI;
> 	print "Hello World";
> 	my Curses::UI $cui = new Curses::UI;
> 	print "!$cui!\n";
> And again I got nothing. That's got me somehow confused.
> The thing is, writing print "Hello World\n"; outputs it, so perl somehow 
> decides to stop on this my line. It returns a false value on comman line, but 
> gives no warning or error.
> 
> Is this expected behaviour?

The above should work.  In fact, it does.  Curses::UI->new seems to
be clearing the screen or doing something funky with the terminal when I run 
it.  So all you're left with is "Hello World" on the screen.  But the
object is there.

Here's an example with something that doesn't mess with the screen: the 
URI module.

  use strict;
  use URI;
  print "Hello World\n";
  my URI $uri = new URI;
  print "!$uri!\n";

That prints

Hello World
!!

The object is empty because URI has stringification overloaded.  If I'd
instead said:

#!/usr/bin/perl

use strict;
use URI;
print "Hello World\n";
my URI $uri = new URI "http://www.perl.org/";
print "!$uri!\n";

It prints
Hello World
|http://www.perl.org/!


-- 
Michael G Schwern        schwern@pobox.com  http://www.pobox.com/~schwern/
The Power almighty rests in this Lone Ring.
The Power, alrighty, for doing your Own Thing.
If broken or busted, it cannot be remade.
If found, send to Sorhed (the postage is prepaid).
        -- "Bored of the Rings"

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About